From c1dcfa297f6b5b3e2e3d80291e9df44e86b51560 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Mon, 17 Jan 2022 16:41:36 -0500 Subject: [PATCH] Add FileUtils class methods to script DSL - close #144 --- build_tests/typical/fileutils_methods.rb | 11 +++++++++ doc/user_guide.md | 27 ++++++++++++++++++++++ lib/rscons/script.rb | 29 ++++++++++++++++++++++++ spec/build_tests_spec.rb | 12 ++++++++++ 4 files changed, 79 insertions(+) create mode 100644 build_tests/typical/fileutils_methods.rb diff --git a/build_tests/typical/fileutils_methods.rb b/build_tests/typical/fileutils_methods.rb new file mode 100644 index 0000000..e5f5459 --- /dev/null +++ b/build_tests/typical/fileutils_methods.rb @@ -0,0 +1,11 @@ +build do + mkdir "foo" + cd "foo" do + mkdir_p ["bar/baz", "bar/booz"] + end + mv "foo/bar", "foobar" + rmdir "foo" + touch "foobar/booz/a.txt" + cp "foobar/booz/a.txt", "foobar/baz/b.txt" + rm_rf "foobar/booz" +end diff --git a/doc/user_guide.md b/doc/user_guide.md index 36d1882..c0eedfe 100644 --- a/doc/user_guide.md +++ b/doc/user_guide.md @@ -233,6 +233,33 @@ The `Rsconscript` file is a Ruby script. * `rscons` (see ${#Using Subsidiary Build Scripts: The rscons Method}) * `sh` (see (${#Executing Commands: The sh Method}) +Additionally, the following methods from the Ruby +[FileUtils](https://ruby-doc.org/stdlib-3.1.0/libdoc/fileutils/rdoc/FileUtils.html) +module are made available for the build script to call directly: + + * `cd` + * `chmod` + * `chmod_R` + * `chown` + * `chown_R` + * `cp` + * `cp_lr` + * `cp_r` + * `install` + * `ln` + * `ln_s` + * `ln_sf` + * `mkdir` + * `mkdir_p` + * `mv` + * `pwd` + * `rm` + * `rm_f` + * `rm_r` + * `rm_rf` + * `rmdir` + * `touch` + ###> Finding Files: The glob Method The [`glob`](../yard/Rscons/Script/GlobalDsl.html#glob-instance_method) method can be diff --git a/lib/rscons/script.rb b/lib/rscons/script.rb index bdf1e84..5bf0993 100644 --- a/lib/rscons/script.rb +++ b/lib/rscons/script.rb @@ -159,6 +159,35 @@ module Rscons end end + [ + :cd, + :chmod, + :chmod_R, + :chown, + :chown_R, + :cp, + :cp_lr, + :cp_r, + :install, + :ln, + :ln_s, + :ln_sf, + :mkdir, + :mkdir_p, + :mv, + :pwd, + :rm, + :rm_f, + :rm_r, + :rm_rf, + :rmdir, + :touch, + ].each do |method| + define_method(method) do |*args, **kwargs, &block| + FileUtils.__send__(method, *args, **kwargs, &block) + end + end + end # Top-level DSL available to the Rsconscript. diff --git a/spec/build_tests_spec.rb b/spec/build_tests_spec.rb index 8c199f7..3ee6e12 100644 --- a/spec/build_tests_spec.rb +++ b/spec/build_tests_spec.rb @@ -2844,4 +2844,16 @@ EOF end end + context "FileUtils methods" do + it "defines FileUtils methods to be available in the build script" do + test_dir "typical" + result = run_rscons(rsconscript: "fileutils_methods.rb") + expect(result.stderr).to eq "" + expect(result.status).to eq 0 + expect(Dir.exist?("foobar")).to be_truthy + expect(Dir.exist?("foo")).to be_falsey + expect(File.exist?("foobar/baz/b.txt")).to be_truthy + end + end + end