Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --dry-run option to fpm publish #918

Merged
merged 7 commits into from
Jun 3, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 24 additions & 12 deletions src/fpm/cmd/publish.f90
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module fpm_cmd_publish
use fpm_model, only: fpm_model_t
use fpm_error, only: error_t, fpm_stop
use fpm_versioning, only: version_t
use fpm_filesystem, only: exists, join_path, get_temp_filename
use fpm_filesystem, only: exists, join_path, get_temp_filename, delete_file
use fpm_git, only: git_archive
use fpm_downloader, only: downloader_t
use fpm_strings, only: string_t
Expand Down Expand Up @@ -64,19 +64,19 @@ subroutine cmd_publish(settings)
end if
end do

tmp_file = get_temp_filename()
call git_archive('.', tmp_file, error)
if (allocated(error)) call fpm_stop(1, '*cmd_publish* Archive error: '//error%message)

upload_data = [ &
string_t('package_name="'//package%name//'"'), &
string_t('package_license="'//package%license//'"'), &
string_t('package_version="'//version%s()//'"') &
& ]
& string_t('package_name="'//package%name//'"'), &
& string_t('package_license="'//package%license//'"'), &
& string_t('package_version="'//version%s()//'"'), &
& string_t('tarball=@"'//tmp_file//'"') &
& ]

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

tmp_file = get_temp_filename()
call git_archive('.', tmp_file, error)
if (allocated(error)) call fpm_stop(1, '*cmd_publish* Pack error: '//error%message)
upload_data = [upload_data, string_t('tarball=@"'//tmp_file//'"')]

if (settings%show_upload_data) then
do i = 1, size(upload_data)
print *, upload_data(i)%s
Expand All @@ -86,12 +86,24 @@ subroutine cmd_publish(settings)

! Make sure a token is provided for publishing.
if (allocated(settings%token)) then
if (settings%token == '') call fpm_stop(1, 'No token provided.')
if (settings%token == '') then
call delete_file(tmp_file); call fpm_stop(1, 'No token provided.')
end if
else
call fpm_stop(1, 'No token provided.')
call delete_file(tmp_file); call fpm_stop(1, 'No token provided.')
end if

! Perform network request and validate package on the backend as soon as
! https://github.com/fortran-lang/registry/issues/41 is resolved.
if (settings%is_dry_run) then
print *, 'Dry run successful.'
print *, ''
print *, 'tarball generated for upload: ', tmp_file
return
end if

call downloader%upload_form(official_registry_base_url//'/packages', upload_data, error)
call delete_file(tmp_file)
if (allocated(error)) call fpm_stop(1, '*cmd_publish* Upload error: '//error%message)
end
end
2 changes: 1 addition & 1 deletion src/fpm/git.f90
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ subroutine git_archive(source, destination, error)
call fatal_error(error, "Cannot find a suitable archive format for 'git archive'."); return
end if

call execute_command_line('git archive HEAD --format='//archive_format//' -o '// destination, exitstat=stat)
call execute_command_line('git archive HEAD --format='//archive_format//' -o '//destination, exitstat=stat)
if (stat /= 0) then
call fatal_error(error, "Error packing '"//source//"'."); return
end if
Expand Down
12 changes: 10 additions & 2 deletions src/fpm_command_line.f90
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ module fpm_command_line
type, extends(fpm_build_settings) :: fpm_publish_settings
logical :: show_package_version = .false.
logical :: show_upload_data = .false.
logical :: is_dry_run = .false.
character(len=:), allocatable :: token
end type

Expand Down Expand Up @@ -621,6 +622,7 @@ subroutine get_command_line_settings(cmd_settings)
call set_args(common_args // compiler_args //'&
& --show-package-version F &
& --show-upload-data F &
& --dry-run F &
& --token " " &
& --list F &
& --show-model F &
Expand All @@ -638,6 +640,7 @@ subroutine get_command_line_settings(cmd_settings)
cmd_settings = fpm_publish_settings( &
& show_package_version = lget('show-package-version'), &
& show_upload_data = lget('show-upload-data'), &
& is_dry_run = lget('dry-run'), &
& profile=val_profile,&
& prune=.not.lget('no-prune'), &
& compiler=val_compiler, &
Expand Down Expand Up @@ -754,7 +757,8 @@ subroutine set_help()
' install [--profile PROF] [--flag FFLAGS] [--no-rebuild] [--prefix PATH] ', &
' [options] ', &
' clean [--skip] [--all] ', &
' publish [--show-package-version] [--show-upload-data] [--token TOKEN] ', &
' publish [--token TOKEN] [--show-package-version] [--show-upload-data] ', &
' [--dry-run] ', &
' ']
help_usage=[character(len=80) :: &
'' ]
Expand Down Expand Up @@ -878,7 +882,8 @@ subroutine set_help()
' install [--profile PROF] [--flag FFLAGS] [--no-rebuild] [--prefix PATH] ', &
' [options] ', &
' clean [--skip] [--all] ', &
' publish [--show-package-version] [--show-upload-data] [--token TOKEN] ', &
' publish [--token TOKEN] [--show-package-version] [--show-upload-data] ', &
' [--dry-run] ', &
' ', &
'SUBCOMMAND OPTIONS ', &
' -C, --directory PATH', &
Expand Down Expand Up @@ -1362,6 +1367,7 @@ subroutine set_help()
'', &
'SYNOPSIS', &
' fpm publish [--token TOKEN] [--show-package-version] [--show-upload-data]', &
' [--dry-run] ', &
'', &
' fpm publish --help|--version', &
'', &
Expand Down Expand Up @@ -1390,13 +1396,15 @@ subroutine set_help()
'OPTIONS', &
' --show-package-version show package version without publishing', &
' --show-upload-data show uploaded data without publishing', &
' --dry-run create tarball for revision without publishing', &
' --help print this help and exit', &
' --version print program version information and exit', &
'', &
'EXAMPLES', &
'', &
' fpm publish --show-package-version # show package version without publishing', &
' fpm publish --show-upload-data # show upload data without publishing', &
' fpm publish --dry-run # create tarball without publishing', &
' fpm publish --token TOKEN # upload package to the registry using TOKEN', &
'' ]
end subroutine set_help
Expand Down
9 changes: 8 additions & 1 deletion test/cli_test/cli_test.f90
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ program main
logical :: c_a,act_c_a ; namelist/act_cli/act_c_a
logical :: show_v,act_show_v ; namelist/act_cli/act_show_v
logical :: show_u_d,act_show_u_d; namelist/act_cli/act_show_u_d
logical :: dry_run,act_dry_run ; namelist/act_cli/act_dry_run
character(len=:), allocatable :: token, act_token ; namelist/act_cli/act_token

character(len=:), allocatable :: profile,act_profile ; namelist/act_cli/act_profile
character(len=:), allocatable :: args,act_args ; namelist/act_cli/act_args
namelist/expected/cmd,cstat,estat,w_e,w_t,c_s,c_a,name,profile,args,show_v,show_u_d,token
namelist/expected/cmd,cstat,estat,w_e,w_t,c_s,c_a,name,profile,args,show_v,show_u_d,dry_run,token
integer :: lun
logical,allocatable :: tally(:)
logical,allocatable :: subtally(:)
Expand Down Expand Up @@ -76,6 +77,7 @@ program main
'CMD="clean --all", C_A=T, NAME=, ARGS="",', &
'CMD="publish --token abc --show-package-version", SHOW_V=T, NAME=, token="abc",ARGS="",', &
'CMD="publish --token abc --show-upload-data", SHOW_U_D=T, NAME=, token="abc",ARGS="",', &
'CMD="publish --token abc --dry-run", DRY_RUN=T, NAME=, token="abc",ARGS="",', &
'CMD="publish --token abc", NAME=, token="abc",ARGS="",', &
' ' ]
character(len=256) :: readme(3)
Expand Down Expand Up @@ -111,6 +113,7 @@ program main
c_a=.false. ! --all
show_v=.false. ! --show-package-version
show_u_d=.false. ! --show-upload-data
dry_run=.false. ! --dry-run
token='' ! --token TOKEN
args=repeat(' ',132) ! -- ARGS
cmd=repeat(' ',132) ! the command line arguments to test
Expand All @@ -133,6 +136,7 @@ program main
act_c_a=.false.
act_show_v=.false.
act_show_u_d=.false.
act_dry_run=.false.
act_token=''
act_args=repeat(' ',132)
read(lun,nml=act_cli,iostat=ios,iomsg=message)
Expand All @@ -149,6 +153,7 @@ program main
call test_test('WITH_TEST',act_w_t.eqv.w_t)
call test_test('SHOW-PACKAGE-VERSION',act_show_v.eqv.show_v)
call test_test('SHOW-UPLOAD-DATA',act_show_u_d.eqv.show_u_d)
call test_test('DRY-RUN',act_dry_run.eqv.dry_run)
call test_test('TOKEN',act_token==token)
call test_test('ARGS',act_args==args)
if(all(subtally))then
Expand Down Expand Up @@ -238,6 +243,7 @@ subroutine parse()
act_c_a=.false.
act_show_v=.false.
act_show_u_d=.false.
act_dry_run=.false.
act_token=''
act_profile=''

Expand All @@ -263,6 +269,7 @@ subroutine parse()
type is (fpm_publish_settings)
act_show_v=settings%show_package_version
act_show_u_d=settings%show_upload_data
act_dry_run=settings%is_dry_run
act_token=settings%token
end select

Expand Down