Skip to content

Commit 7bec950

Browse files
committed
add clean command
1 parent 0806c9e commit 7bec950

File tree

3 files changed

+69
-11
lines changed

3 files changed

+69
-11
lines changed

app/main.f90

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ program main
88
fpm_test_settings, &
99
fpm_install_settings, &
1010
fpm_update_settings, &
11+
fpm_clean_settings, &
1112
get_command_line_settings
1213
use fpm_error, only: error_t
1314
use fpm_filesystem, only: exists, parent_dir, join_path
14-
use fpm, only: cmd_build, cmd_run
15+
use fpm, only: cmd_build, cmd_run, cmd_clean
1516
use fpm_cmd_install, only: cmd_install
1617
use fpm_cmd_new, only: cmd_new
1718
use fpm_cmd_update, only : cmd_update
@@ -73,6 +74,8 @@ program main
7374
call cmd_install(settings)
7475
type is (fpm_update_settings)
7576
call cmd_update(settings)
77+
type is (fpm_clean_settings)
78+
call cmd_clean(settings)
7679
end select
7780

7881
if (allocated(project_root)) then

src/fpm.f90

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
module fpm
2-
use fpm_strings, only: string_t, operator(.in.), glob, join, string_cat, fnv_1a
2+
use fpm_strings, only: string_t, operator(.in.), glob, join, string_cat, fnv_1a, lower
33
use fpm_backend, only: build_package
44
use fpm_command_line, only: fpm_build_settings, fpm_new_settings, &
5-
fpm_run_settings, fpm_install_settings, fpm_test_settings
5+
fpm_run_settings, fpm_install_settings, fpm_test_settings, &
6+
fpm_clean_settings
67
use fpm_dependency, only : new_dependency_tree
78
use fpm_environment, only: get_env
89
use fpm_filesystem, only: is_dir, join_path, number_of_rows, list_files, exists, &
@@ -24,7 +25,7 @@ module fpm
2425
& stderr=>error_unit
2526
implicit none
2627
private
27-
public :: cmd_build, cmd_run
28+
public :: cmd_build, cmd_run, cmd_clean
2829
public :: build_model, check_modules_for_duplicates
2930

3031
contains
@@ -502,4 +503,24 @@ end subroutine compact_list
502503

503504
end subroutine cmd_run
504505

506+
subroutine cmd_clean(settings)
507+
class(fpm_clean_settings), intent(in) :: settings
508+
character(len=1) :: response
509+
if (is_dir("build")) then
510+
write(stdout, '(A)', advance='no') "Delete the build directory (y/n)? "
511+
read(stdin, '(A1)') response
512+
if (lower(response) == 'y') then
513+
if(settings%unix) then
514+
call run('rm -rf build', .false.)
515+
else
516+
call run('rmdir /s/q build', .false.)
517+
end if
518+
else
519+
write (stdout, '(A)') "fpm: The response was not affirmative, build directory was not deleted."
520+
end if
521+
else
522+
write (stdout, '(A)') "fpm: No build directory found."
523+
end if
524+
end subroutine cmd_clean
525+
505526
end module fpm

src/fpm_command_line.f90

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
!> ``fpm-help`` and ``fpm --list`` help pages below to make sure the help output
2424
!> is complete and consistent as well.
2525
module fpm_command_line
26-
use fpm_environment, only : get_os_type, get_env, &
26+
use fpm_environment, only : get_os_type, get_env, os_is_unix, &
2727
OS_UNKNOWN, OS_LINUX, OS_MACOS, OS_WINDOWS, &
2828
OS_CYGWIN, OS_SOLARIS, OS_FREEBSD, OS_OPENBSD
2929
use M_CLI2, only : set_args, lget, sget, unnamed, remaining, specified
@@ -47,6 +47,7 @@ module fpm_command_line
4747
fpm_run_settings, &
4848
fpm_test_settings, &
4949
fpm_update_settings, &
50+
fpm_clean_settings, &
5051
get_command_line_settings
5152

5253
type, abstract :: fpm_cmd_settings
@@ -104,6 +105,10 @@ module fpm_command_line
104105
logical :: clean
105106
end type
106107

108+
type, extends(fpm_cmd_settings) :: fpm_clean_settings
109+
logical :: unix
110+
end type
111+
107112
character(len=:),allocatable :: name
108113
character(len=:),allocatable :: os_type
109114
character(len=ibug),allocatable :: names(:)
@@ -113,9 +118,10 @@ module fpm_command_line
113118
character(len=:), allocatable :: help_new(:), help_fpm(:), help_run(:), &
114119
& help_test(:), help_build(:), help_usage(:), help_runner(:), &
115120
& help_text(:), help_install(:), help_help(:), help_update(:), &
116-
& help_list(:), help_list_dash(:), help_list_nodash(:)
121+
& help_list(:), help_list_dash(:), help_list_nodash(:), &
122+
& help_clean(:)
117123
character(len=20),parameter :: manual(*)=[ character(len=20) ::&
118-
& ' ', 'fpm', 'new', 'build', 'run', &
124+
& ' ', 'fpm', 'new', 'build', 'run', 'clean', &
119125
& 'test', 'runner', 'install', 'update', 'list', 'help', 'version' ]
120126

121127
character(len=:), allocatable :: val_runner, val_compiler, val_flag, val_cflag, val_ldflag, &
@@ -174,6 +180,8 @@ subroutine get_command_line_settings(cmd_settings)
174180
character(len=4096) :: cmdarg
175181
integer :: i
176182
integer :: widest
183+
integer :: os
184+
logical :: unix
177185
type(fpm_install_settings), allocatable :: install_settings
178186
character(len=:), allocatable :: common_args, compiler_args, run_args, working_dir, &
179187
& c_compiler, archiver
@@ -184,8 +192,9 @@ subroutine get_command_line_settings(cmd_settings)
184192
type(error_t), allocatable :: error
185193

186194
call set_help()
195+
os = get_os_type()
187196
! text for --version switch,
188-
select case (get_os_type())
197+
select case (os)
189198
case (OS_LINUX); os_type = "OS Type: Linux"
190199
case (OS_MACOS); os_type = "OS Type: macOS"
191200
case (OS_WINDOWS); os_type = "OS Type: Windows"
@@ -196,6 +205,7 @@ subroutine get_command_line_settings(cmd_settings)
196205
case (OS_UNKNOWN); os_type = "OS Type: Unknown"
197206
case default ; os_type = "OS Type: UNKNOWN"
198207
end select
208+
unix = os_is_unix(os)
199209
version_text = [character(len=80) :: &
200210
& 'Version: 0.5.0, alpha', &
201211
& 'Program: fpm(1)', &
@@ -321,7 +331,7 @@ subroutine get_command_line_settings(cmd_settings)
321331
select case(size(unnamed))
322332
case(1)
323333
if(lget('backfill'))then
324-
name='.'
334+
name='.'
325335
else
326336
write(stderr,'(*(7x,g0,/))') &
327337
& '<USAGE> fpm new NAME [[--lib|--src] [--app] [--test] [--example]]|[--full|--bare] [--backfill]'
@@ -424,6 +434,8 @@ subroutine get_command_line_settings(cmd_settings)
424434
help_text=[character(len=widest) :: help_text, help_help]
425435
case('version' )
426436
help_text=[character(len=widest) :: help_text, version_text]
437+
case('clean' )
438+
help_text=[character(len=widest) :: help_text, help_clean]
427439
case default
428440
help_text=[character(len=widest) :: help_text, &
429441
& '<ERROR> unknown help topic "'//trim(unnamed(i))//'"']
@@ -528,6 +540,11 @@ subroutine get_command_line_settings(cmd_settings)
528540
fetch_only=lget('fetch-only'), verbose=lget('verbose'), &
529541
clean=lget('clean'))
530542

543+
case('clean')
544+
call set_args(common_args, help_clean)
545+
allocate(fpm_clean_settings :: cmd_settings)
546+
cmd_settings=fpm_clean_settings(unix=unix)
547+
531548
case default
532549

533550
if(which('fpm-'//cmdarg).ne.'')then
@@ -607,6 +624,7 @@ subroutine set_help()
607624
' test Run the test programs ', &
608625
' update Update and manage project dependencies ', &
609626
' install Install project ', &
627+
' clean Delete the "build" directory ', &
610628
' ', &
611629
' Enter "fpm --list" for a brief list of subcommand options. Enter ', &
612630
' "fpm --help" or "fpm SUBCOMMAND --help" for detailed descriptions. ', &
@@ -728,6 +746,7 @@ subroutine set_help()
728746
' + help Alternate to the --help switch for displaying help text. ', &
729747
' + list Display brief descriptions of all subcommands. ', &
730748
' + install Install project ', &
749+
' + clean Delete the "build" directory ', &
731750
' ', &
732751
' Their syntax is ', &
733752
' ', &
@@ -743,7 +762,8 @@ subroutine set_help()
743762
' help [NAME(s)] ', &
744763
' list [--list] ', &
745764
' install [--profile PROF] [--flag FFLAGS] [--no-rebuild] [--prefix PATH] ', &
746-
' [options] ', &
765+
' [options] ', &
766+
' clean ', &
747767
' ', &
748768
'SUBCOMMAND OPTIONS ', &
749769
' -C, --directory PATH', &
@@ -809,6 +829,7 @@ subroutine set_help()
809829
' fpm new --help ', &
810830
' fpm run myprogram --profile release -- -x 10 -y 20 --title "my title" ', &
811831
' fpm install --prefix ~/.local ', &
832+
' fpm clean ', &
812833
' ', &
813834
'SEE ALSO ', &
814835
' ', &
@@ -1219,7 +1240,20 @@ subroutine set_help()
12191240
'', &
12201241
' fpm install --prefix $PWD --bindir exe', &
12211242
'' ]
1222-
end subroutine set_help
1243+
help_clean=[character(len=80) :: &
1244+
'NAME', &
1245+
' clean(1) - delete the "build" directory', &
1246+
'', &
1247+
'SYNOPSIS', &
1248+
' fpm clean', &
1249+
'', &
1250+
'DESCRIPTION', &
1251+
' Prompts the user to confirm deletion of the "build" directory. If affirmative,', &
1252+
' the "build" directory in the project root is deleted using os system specific', &
1253+
' commands, forcing the recursive removal of all files and directories,', &
1254+
' including dependencies.', &
1255+
'' ]
1256+
end subroutine set_help
12231257

12241258
subroutine get_char_arg(var, arg)
12251259
character(len=:), allocatable, intent(out) :: var

0 commit comments

Comments
 (0)