Skip to content

Commit b20b3dc

Browse files
authored
Merge pull request #918 from fortran-lang/add-dry-run-to-publish
Add `--dry-run` option to `fpm publish`
2 parents 08523b5 + cc7cedb commit b20b3dc

File tree

4 files changed

+64
-26
lines changed

4 files changed

+64
-26
lines changed

src/fpm/cmd/publish.f90

+38-16
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module fpm_cmd_publish
88
use fpm_model, only: fpm_model_t
99
use fpm_error, only: error_t, fpm_stop
1010
use fpm_versioning, only: version_t
11-
use fpm_filesystem, only: exists, join_path, get_temp_filename
11+
use fpm_filesystem, only: exists, join_path, get_temp_filename, delete_file
1212
use fpm_git, only: git_archive
1313
use fpm_downloader, only: downloader_t
1414
use fpm_strings, only: string_t
@@ -64,34 +64,56 @@ subroutine cmd_publish(settings)
6464
end if
6565
end do
6666

67+
tmp_file = get_temp_filename()
68+
call git_archive('.', tmp_file, error)
69+
if (allocated(error)) call fpm_stop(1, '*cmd_publish* Archive error: '//error%message)
70+
6771
upload_data = [ &
68-
string_t('package_name="'//package%name//'"'), &
69-
string_t('package_license="'//package%license//'"'), &
70-
string_t('package_version="'//version%s()//'"') &
71-
& ]
72+
& string_t('package_name="'//package%name//'"'), &
73+
& string_t('package_license="'//package%license//'"'), &
74+
& string_t('package_version="'//version%s()//'"'), &
75+
& string_t('tarball=@"'//tmp_file//'"') &
76+
& ]
7277

7378
if (allocated(settings%token)) upload_data = [upload_data, string_t('upload_token="'//settings%token//'"')]
7479

75-
tmp_file = get_temp_filename()
76-
call git_archive('.', tmp_file, error)
77-
if (allocated(error)) call fpm_stop(1, '*cmd_publish* Pack error: '//error%message)
78-
upload_data = [upload_data, string_t('tarball=@"'//tmp_file//'"')]
79-
8080
if (settings%show_upload_data) then
81-
do i = 1, size(upload_data)
82-
print *, upload_data(i)%s
83-
end do
84-
return
81+
call print_upload_data(upload_data); return
8582
end if
8683

8784
! Make sure a token is provided for publishing.
8885
if (allocated(settings%token)) then
89-
if (settings%token == '') call fpm_stop(1, 'No token provided.')
86+
if (settings%token == '') then
87+
call delete_file(tmp_file); call fpm_stop(1, 'No token provided.')
88+
end if
9089
else
91-
call fpm_stop(1, 'No token provided.')
90+
call delete_file(tmp_file); call fpm_stop(1, 'No token provided.')
91+
end if
92+
93+
if (settings%verbose) then
94+
print *, ''
95+
call print_upload_data(upload_data)
96+
print *, ''
97+
end if
98+
99+
! Perform network request and validate package, token etc. on the backend once
100+
! https://github.com/fortran-lang/registry/issues/41 is resolved.
101+
if (settings%is_dry_run) then
102+
print *, 'Dry run successful. Generated tarball: ', tmp_file; return
92103
end if
93104

94105
call downloader%upload_form(official_registry_base_url//'/packages', upload_data, error)
106+
call delete_file(tmp_file)
95107
if (allocated(error)) call fpm_stop(1, '*cmd_publish* Upload error: '//error%message)
96108
end
109+
110+
subroutine print_upload_data(upload_data)
111+
type(string_t), intent(in) :: upload_data(:)
112+
integer :: i
113+
114+
print *, 'Upload data:'
115+
do i = 1, size(upload_data)
116+
print *, upload_data(i)%s
117+
end do
118+
end
97119
end

src/fpm/git.f90

+1-1
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ subroutine git_archive(source, destination, error)
328328
call fatal_error(error, "Cannot find a suitable archive format for 'git archive'."); return
329329
end if
330330

331-
call execute_command_line('git archive HEAD --format='//archive_format//' -o '// destination, exitstat=stat)
331+
call execute_command_line('git archive HEAD --format='//archive_format//' -o '//destination, exitstat=stat)
332332
if (stat /= 0) then
333333
call fatal_error(error, "Error packing '"//source//"'."); return
334334
end if

src/fpm_command_line.f90

+17-8
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ module fpm_command_line
121121
type, extends(fpm_build_settings) :: fpm_publish_settings
122122
logical :: show_package_version = .false.
123123
logical :: show_upload_data = .false.
124+
logical :: is_dry_run = .false.
124125
character(len=:), allocatable :: token
125126
end type
126127

@@ -168,7 +169,7 @@ module fpm_command_line
168169
' --flag FFLAGS selects compile arguments for the build, the default value is',&
169170
' set by the FPM_FFLAGS environment variable. These are added ',&
170171
' to the profile options if --profile is specified, else these ',&
171-
' these options override the defaults. Note object and .mod ',&
172+
' options override the defaults. Note object and .mod ',&
172173
' directory locations are always built in. ',&
173174
' --c-flag CFLAGS selects compile arguments specific for C source in the build.',&
174175
' The default value is set by the FPM_CFLAGS environment ',&
@@ -621,6 +622,7 @@ subroutine get_command_line_settings(cmd_settings)
621622
call set_args(common_args // compiler_args //'&
622623
& --show-package-version F &
623624
& --show-upload-data F &
625+
& --dry-run F &
624626
& --token " " &
625627
& --list F &
626628
& --show-model F &
@@ -638,6 +640,7 @@ subroutine get_command_line_settings(cmd_settings)
638640
cmd_settings = fpm_publish_settings( &
639641
& show_package_version = lget('show-package-version'), &
640642
& show_upload_data = lget('show-upload-data'), &
643+
& is_dry_run = lget('dry-run'), &
641644
& profile=val_profile,&
642645
& prune=.not.lget('no-prune'), &
643646
& compiler=val_compiler, &
@@ -754,7 +757,8 @@ subroutine set_help()
754757
' install [--profile PROF] [--flag FFLAGS] [--no-rebuild] [--prefix PATH] ', &
755758
' [options] ', &
756759
' clean [--skip] [--all] ', &
757-
' publish [--show-package-version] [--show-upload-data] [--token TOKEN] ', &
760+
' publish [--token TOKEN] [--show-package-version] [--show-upload-data] ', &
761+
' [--dry-run] [--verbose] ', &
758762
' ']
759763
help_usage=[character(len=80) :: &
760764
'' ]
@@ -878,7 +882,8 @@ subroutine set_help()
878882
' install [--profile PROF] [--flag FFLAGS] [--no-rebuild] [--prefix PATH] ', &
879883
' [options] ', &
880884
' clean [--skip] [--all] ', &
881-
' publish [--show-package-version] [--show-upload-data] [--token TOKEN] ', &
885+
' publish [--token TOKEN] [--show-package-version] [--show-upload-data] ', &
886+
' [--dry-run] [--verbose] ', &
882887
' ', &
883888
'SUBCOMMAND OPTIONS ', &
884889
' -C, --directory PATH', &
@@ -1362,6 +1367,7 @@ subroutine set_help()
13621367
'', &
13631368
'SYNOPSIS', &
13641369
' fpm publish [--token TOKEN] [--show-package-version] [--show-upload-data]', &
1370+
' [--dry-run] [--verbose] ', &
13651371
'', &
13661372
' fpm publish --help|--version', &
13671373
'', &
@@ -1379,7 +1385,7 @@ subroutine set_help()
13791385
' But be aware that the upload is permanent. An uploaded package cannot be', &
13801386
' deleted.', &
13811387
'', &
1382-
' See documentation for more information regarding the package upload and usage:', &
1388+
' See documentation for more information regarding package upload and usage:', &
13831389
'', &
13841390
' Package upload:', &
13851391
' https://fpm.fortran-lang.org/en/spec/publish.html', &
@@ -1389,15 +1395,18 @@ subroutine set_help()
13891395
'', &
13901396
'OPTIONS', &
13911397
' --show-package-version show package version without publishing', &
1392-
' --show-upload-data show uploaded data without publishing', &
1398+
' --show-upload-data show upload data without publishing', &
1399+
' --dry-run perform dry run without publishing', &
13931400
' --help print this help and exit', &
13941401
' --version print program version information and exit', &
1402+
' --verbose print more information', &
13951403
'', &
13961404
'EXAMPLES', &
13971405
'', &
1398-
' fpm publish --show-package-version # show package version without publishing', &
1399-
' fpm publish --show-upload-data # show upload data without publishing', &
1400-
' fpm publish --token TOKEN # upload package to the registry using TOKEN', &
1406+
' fpm publish --show-package-version # show package version without publishing', &
1407+
' fpm publish --show-upload-data # show upload data without publishing', &
1408+
' fpm publish --token TOKEN --dry-run # perform dry run without publishing', &
1409+
' fpm publish --token TOKEN # upload package to the registry', &
14011410
'' ]
14021411
end subroutine set_help
14031412

test/cli_test/cli_test.f90

+8-1
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,12 @@ program main
3131
logical :: c_a,act_c_a ; namelist/act_cli/act_c_a
3232
logical :: show_v,act_show_v ; namelist/act_cli/act_show_v
3333
logical :: show_u_d,act_show_u_d; namelist/act_cli/act_show_u_d
34+
logical :: dry_run,act_dry_run ; namelist/act_cli/act_dry_run
3435
character(len=:), allocatable :: token, act_token ; namelist/act_cli/act_token
3536

3637
character(len=:), allocatable :: profile,act_profile ; namelist/act_cli/act_profile
3738
character(len=:), allocatable :: args,act_args ; namelist/act_cli/act_args
38-
namelist/expected/cmd,cstat,estat,w_e,w_t,c_s,c_a,name,profile,args,show_v,show_u_d,token
39+
namelist/expected/cmd,cstat,estat,w_e,w_t,c_s,c_a,name,profile,args,show_v,show_u_d,dry_run,token
3940
integer :: lun
4041
logical,allocatable :: tally(:)
4142
logical,allocatable :: subtally(:)
@@ -76,6 +77,7 @@ program main
7677
'CMD="clean --all", C_A=T, NAME=, ARGS="",', &
7778
'CMD="publish --token abc --show-package-version", SHOW_V=T, NAME=, token="abc",ARGS="",', &
7879
'CMD="publish --token abc --show-upload-data", SHOW_U_D=T, NAME=, token="abc",ARGS="",', &
80+
'CMD="publish --token abc --dry-run", DRY_RUN=T, NAME=, token="abc",ARGS="",', &
7981
'CMD="publish --token abc", NAME=, token="abc",ARGS="",', &
8082
' ' ]
8183
character(len=256) :: readme(3)
@@ -111,6 +113,7 @@ program main
111113
c_a=.false. ! --all
112114
show_v=.false. ! --show-package-version
113115
show_u_d=.false. ! --show-upload-data
116+
dry_run=.false. ! --dry-run
114117
token='' ! --token TOKEN
115118
args=repeat(' ',132) ! -- ARGS
116119
cmd=repeat(' ',132) ! the command line arguments to test
@@ -133,6 +136,7 @@ program main
133136
act_c_a=.false.
134137
act_show_v=.false.
135138
act_show_u_d=.false.
139+
act_dry_run=.false.
136140
act_token=''
137141
act_args=repeat(' ',132)
138142
read(lun,nml=act_cli,iostat=ios,iomsg=message)
@@ -149,6 +153,7 @@ program main
149153
call test_test('WITH_TEST',act_w_t.eqv.w_t)
150154
call test_test('SHOW-PACKAGE-VERSION',act_show_v.eqv.show_v)
151155
call test_test('SHOW-UPLOAD-DATA',act_show_u_d.eqv.show_u_d)
156+
call test_test('DRY-RUN',act_dry_run.eqv.dry_run)
152157
call test_test('TOKEN',act_token==token)
153158
call test_test('ARGS',act_args==args)
154159
if(all(subtally))then
@@ -238,6 +243,7 @@ subroutine parse()
238243
act_c_a=.false.
239244
act_show_v=.false.
240245
act_show_u_d=.false.
246+
act_dry_run=.false.
241247
act_token=''
242248
act_profile=''
243249

@@ -263,6 +269,7 @@ subroutine parse()
263269
type is (fpm_publish_settings)
264270
act_show_v=settings%show_package_version
265271
act_show_u_d=settings%show_upload_data
272+
act_dry_run=settings%is_dry_run
266273
act_token=settings%token
267274
end select
268275

0 commit comments

Comments
 (0)