fedux_org-stdlib 0.3.2 → 0.6.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (175) hide show
  1. checksums.yaml +7 -0
  2. data/.rspec +2 -0
  3. data/.simplecov +7 -6
  4. data/Gemfile +33 -28
  5. data/README.md +1 -1
  6. data/Rakefile +8 -1
  7. data/doc/colors.md +2 -2
  8. data/doc/commands.md +2 -2
  9. data/doc/logic_converters.md +5 -5
  10. data/doc/models.md +5 -5
  11. data/fedux_org-stdlib.gemspec +3 -7
  12. data/lib/fedux_org_stdlib.rb +13 -0
  13. data/lib/fedux_org_stdlib/app_config.rb +247 -0
  14. data/lib/fedux_org_stdlib/ci.rb +6 -0
  15. data/lib/fedux_org_stdlib/colors/html_color.rb +18 -0
  16. data/lib/fedux_org_stdlib/command.rb +10 -0
  17. data/lib/fedux_org_stdlib/command/command_result.rb +13 -0
  18. data/lib/fedux_org_stdlib/command/run_command.rb +55 -0
  19. data/lib/fedux_org_stdlib/command/which.rb +60 -0
  20. data/lib/fedux_org_stdlib/core_ext/array.rb +6 -0
  21. data/lib/fedux_org_stdlib/core_ext/string.rb +3 -0
  22. data/lib/fedux_org_stdlib/environment.rb +32 -0
  23. data/lib/fedux_org_stdlib/filesystem.rb +248 -0
  24. data/lib/fedux_org_stdlib/filesystem/exceptions.rb +12 -0
  25. data/lib/fedux_org_stdlib/logging.rb +12 -0
  26. data/lib/fedux_org_stdlib/logging/logger.rb +79 -0
  27. data/lib/{fedux_org/stdlib → fedux_org_stdlib}/logging/severity.rb +1 -0
  28. data/lib/fedux_org_stdlib/logic_converters.rb +11 -0
  29. data/lib/{fedux_org/stdlib → fedux_org_stdlib}/logic_converters/exceptions.rb +1 -0
  30. data/lib/fedux_org_stdlib/logic_converters/logic_converter.rb +39 -0
  31. data/lib/fedux_org_stdlib/logic_converters/on_off_converter.rb +35 -0
  32. data/lib/fedux_org_stdlib/logic_converters/true_false_converter.rb +34 -0
  33. data/lib/fedux_org_stdlib/logic_converters/y_n_converter.rb +34 -0
  34. data/lib/fedux_org_stdlib/logic_converters/yes_no_converter.rb +34 -0
  35. data/lib/fedux_org_stdlib/models.rb +5 -0
  36. data/lib/fedux_org_stdlib/models/base_model.rb +129 -0
  37. data/lib/fedux_org_stdlib/models/class_based_model.rb +67 -0
  38. data/lib/fedux_org_stdlib/models/exceptions.rb +40 -0
  39. data/lib/fedux_org_stdlib/models/filesystem_based_model.rb +98 -0
  40. data/lib/fedux_org_stdlib/project.rb +6 -0
  41. data/lib/fedux_org_stdlib/project/generators/taskjuggler.rb +35 -0
  42. data/lib/fedux_org_stdlib/project/logger.rb +12 -0
  43. data/lib/fedux_org_stdlib/project/plan.rb +26 -0
  44. data/lib/fedux_org_stdlib/project/report.rb +63 -0
  45. data/lib/fedux_org_stdlib/rake.rb +8 -0
  46. data/lib/fedux_org_stdlib/rake/exceptions.rb +9 -0
  47. data/lib/{fedux_org/stdlib → fedux_org_stdlib}/rake/project.rb +4 -4
  48. data/lib/fedux_org_stdlib/rake/project_task.rb +29 -0
  49. data/lib/fedux_org_stdlib/rake/shell_task.rb +45 -0
  50. data/lib/fedux_org_stdlib/rake/sub_task.rb +26 -0
  51. data/lib/fedux_org_stdlib/rake/task.rb +118 -0
  52. data/lib/fedux_org_stdlib/rake/version_bump_task.rb +56 -0
  53. data/lib/fedux_org_stdlib/rake_tasks.rb +8 -0
  54. data/lib/fedux_org_stdlib/rake_tasks/console.rb +11 -0
  55. data/lib/fedux_org_stdlib/rake_tasks/documentation.rb +2 -0
  56. data/lib/fedux_org_stdlib/rake_tasks/documentation/yard.rb +9 -0
  57. data/lib/fedux_org_stdlib/rake_tasks/gems.rb +3 -0
  58. data/lib/fedux_org_stdlib/rake_tasks/gems/bundler.rb +9 -0
  59. data/lib/fedux_org_stdlib/rake_tasks/gems/package.rb +9 -0
  60. data/lib/fedux_org_stdlib/rake_tasks/project.rb +1 -0
  61. data/lib/fedux_org_stdlib/rake_tasks/project/report.rb +3 -0
  62. data/lib/fedux_org_stdlib/rake_tasks/project/report/generate.rb +10 -0
  63. data/lib/fedux_org_stdlib/rake_tasks/project/report/open.rb +12 -0
  64. data/lib/fedux_org_stdlib/rake_tasks/project/report/open_shortcut.rb +9 -0
  65. data/lib/fedux_org_stdlib/rake_tasks/qa.rb +4 -0
  66. data/lib/fedux_org_stdlib/rake_tasks/qa/rubocop.rb +13 -0
  67. data/lib/fedux_org_stdlib/rake_tasks/qa/travis.rb +13 -0
  68. data/lib/fedux_org_stdlib/rake_tasks/terminal.rb +9 -0
  69. data/lib/fedux_org_stdlib/rake_tasks/tests.rb +4 -0
  70. data/lib/fedux_org_stdlib/rake_tasks/tests/cucumber.rb +13 -0
  71. data/lib/fedux_org_stdlib/rake_tasks/tests/rspec.rb +12 -0
  72. data/lib/fedux_org_stdlib/rake_tasks/tests/travis.rb +11 -0
  73. data/lib/fedux_org_stdlib/rake_tasks/version.rb +6 -0
  74. data/lib/fedux_org_stdlib/rake_tasks/version/bump.rb +8 -0
  75. data/lib/fedux_org_stdlib/rake_tasks/version/restore.rb +11 -0
  76. data/lib/fedux_org_stdlib/rake_tasks/version/show.rb +12 -0
  77. data/lib/fedux_org_stdlib/rake_tasks/version/show_shortcut.rb +9 -0
  78. data/lib/fedux_org_stdlib/require_files.rb +27 -0
  79. data/lib/fedux_org_stdlib/version.rb +5 -0
  80. data/lib/fedux_org_stdlib/version_management/exceptions.rb +9 -0
  81. data/lib/fedux_org_stdlib/version_management/library_builder.rb +12 -0
  82. data/lib/fedux_org_stdlib/version_management/ruby_library.rb +13 -0
  83. data/lib/fedux_org_stdlib/version_management/rubygem_version_file.rb +78 -0
  84. data/lib/fedux_org_stdlib/version_management/rubygem_version_file_parser.rb +22 -0
  85. data/lib/fedux_org_stdlib/version_management/version.rb +25 -0
  86. data/lib/fedux_org_stdlib/version_management/version_builder.rb +12 -0
  87. data/lib/fedux_org_stdlib/versionable.rb +18 -0
  88. data/rakefiles/default.rake +8 -1
  89. data/rakefiles/travis.rake +1 -1
  90. data/script/console +1 -1
  91. data/script/terminal +3 -2
  92. data/spec/app_config_spec.rb +7 -0
  93. data/spec/colors/html_color_spec.rb +7 -14
  94. data/spec/command/run_command_spec.rb +9 -10
  95. data/spec/command/which_spec.rb +8 -9
  96. data/spec/environment_spec.rb +12 -11
  97. data/spec/logger/logger_spec.rb +17 -16
  98. data/spec/logic_converters/on_off_converter_spec.rb +8 -7
  99. data/spec/logic_converters/true_false_converter_spec.rb +8 -7
  100. data/spec/logic_converters/y_n_converter_spec.rb +8 -7
  101. data/spec/logic_converters/yes_no_converter_spec.rb +8 -7
  102. data/spec/models/base_model_spec.rb +7 -7
  103. data/spec/models/class_based_model_spec.rb +37 -39
  104. data/spec/models/filesystem_based_model_spec.rb +24 -24
  105. data/spec/project/plan_spec.rb +6 -5
  106. data/spec/project/report_spec.rb +35 -29
  107. data/spec/project/taskjuggler_spec.rb +13 -12
  108. data/spec/spec_helper.rb +15 -38
  109. data/spec/support/environment.rb +16 -0
  110. data/spec/support/filesystem.rb +19 -0
  111. data/spec/support/reporting.rb +2 -0
  112. data/spec/support/rspec.rb +5 -0
  113. data/spec/support/string.rb +2 -0
  114. data/spec/version_management/library_builder_spec.rb +5 -4
  115. data/spec/version_management/ruby_library_spec.rb +5 -4
  116. data/spec/version_management/rubygem_version_file_parser_spec.rb +27 -16
  117. data/spec/version_management/rubygem_version_file_spec.rb +56 -54
  118. data/spec/version_management/version_builder_spec.rb +5 -4
  119. data/spec/version_management/version_spec.rb +16 -16
  120. metadata +104 -110
  121. data/lib/fedux_org/stdlib.rb +0 -15
  122. data/lib/fedux_org/stdlib/colors/html_color.rb +0 -19
  123. data/lib/fedux_org/stdlib/command.rb +0 -11
  124. data/lib/fedux_org/stdlib/command/command_result.rb +0 -14
  125. data/lib/fedux_org/stdlib/command/run_command.rb +0 -55
  126. data/lib/fedux_org/stdlib/command/which.rb +0 -67
  127. data/lib/fedux_org/stdlib/environment.rb +0 -31
  128. data/lib/fedux_org/stdlib/filesystem.rb +0 -247
  129. data/lib/fedux_org/stdlib/filesystem/exceptions.rb +0 -13
  130. data/lib/fedux_org/stdlib/logging.rb +0 -13
  131. data/lib/fedux_org/stdlib/logging/logger.rb +0 -79
  132. data/lib/fedux_org/stdlib/logic_converters.rb +0 -12
  133. data/lib/fedux_org/stdlib/logic_converters/logic_converter.rb +0 -40
  134. data/lib/fedux_org/stdlib/logic_converters/on_off_converter.rb +0 -36
  135. data/lib/fedux_org/stdlib/logic_converters/true_false_converter.rb +0 -35
  136. data/lib/fedux_org/stdlib/logic_converters/y_n_converter.rb +0 -35
  137. data/lib/fedux_org/stdlib/logic_converters/yes_no_converter.rb +0 -35
  138. data/lib/fedux_org/stdlib/models.rb +0 -4
  139. data/lib/fedux_org/stdlib/models/base_model.rb +0 -129
  140. data/lib/fedux_org/stdlib/models/class_based_model.rb +0 -76
  141. data/lib/fedux_org/stdlib/models/exceptions.rb +0 -41
  142. data/lib/fedux_org/stdlib/models/filesystem_based_model.rb +0 -99
  143. data/lib/fedux_org/stdlib/project.rb +0 -5
  144. data/lib/fedux_org/stdlib/project/generators/taskjuggler.rb +0 -41
  145. data/lib/fedux_org/stdlib/project/logger.rb +0 -13
  146. data/lib/fedux_org/stdlib/project/plan.rb +0 -15
  147. data/lib/fedux_org/stdlib/project/report.rb +0 -67
  148. data/lib/fedux_org/stdlib/rake.rb +0 -7
  149. data/lib/fedux_org/stdlib/rake/console.rb +0 -3
  150. data/lib/fedux_org/stdlib/rake/documentation.rb +0 -1
  151. data/lib/fedux_org/stdlib/rake/documentation/yard.rb +0 -14
  152. data/lib/fedux_org/stdlib/rake/gems.rb +0 -2
  153. data/lib/fedux_org/stdlib/rake/gems/bundler.rb +0 -12
  154. data/lib/fedux_org/stdlib/rake/gems/package.rb +0 -7
  155. data/lib/fedux_org/stdlib/rake/library.rb +0 -8
  156. data/lib/fedux_org/stdlib/rake/terminal.rb +0 -9
  157. data/lib/fedux_org/stdlib/rake/tests.rb +0 -3
  158. data/lib/fedux_org/stdlib/rake/tests/cucumber.rb +0 -6
  159. data/lib/fedux_org/stdlib/rake/tests/rspec.rb +0 -6
  160. data/lib/fedux_org/stdlib/rake/tests/travis.rb +0 -15
  161. data/lib/fedux_org/stdlib/rake/travis.rb +0 -1
  162. data/lib/fedux_org/stdlib/rake/travis/lint.rb +0 -10
  163. data/lib/fedux_org/stdlib/rake/version.rb +0 -4
  164. data/lib/fedux_org/stdlib/rake/version/base.rb +0 -7
  165. data/lib/fedux_org/stdlib/rake/version/bump.rb +0 -78
  166. data/lib/fedux_org/stdlib/rake/version/restore.rb +0 -9
  167. data/lib/fedux_org/stdlib/rake/version/show.rb +0 -9
  168. data/lib/fedux_org/stdlib/rake/version/show_shortcut.rb +0 -6
  169. data/lib/fedux_org/stdlib/version.rb +0 -6
  170. data/lib/fedux_org/stdlib/version_management/library_builder.rb +0 -15
  171. data/lib/fedux_org/stdlib/version_management/ruby_library.rb +0 -14
  172. data/lib/fedux_org/stdlib/version_management/rubygem_version_file.rb +0 -90
  173. data/lib/fedux_org/stdlib/version_management/rubygem_version_file_parser.rb +0 -18
  174. data/lib/fedux_org/stdlib/version_management/version.rb +0 -33
  175. data/lib/fedux_org/stdlib/version_management/version_builder.rb +0 -15
@@ -0,0 +1,6 @@
1
+ # encoding: utf-8
2
+
3
+ # Running in continous integration mode
4
+ def ci?
5
+ ENV.key? 'CI'
6
+ end
@@ -0,0 +1,18 @@
1
+ # encoding: utf-8
2
+ module FeduxOrgStdlib
3
+ module Colors
4
+ class HtmlColor
5
+ # @param [String] color
6
+ # string in the html color format
7
+ def initialize( color )
8
+ @color = color
9
+ @validator_regex = /^#[a-f0-9]{6}$/
10
+ end
11
+
12
+ # @return [true,false] color string is valid
13
+ def valid?
14
+ @validator_regex === @color
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,10 @@
1
+ # encoding: utf-8
2
+ require 'fedux_org_stdlib/command/run_command'
3
+ require 'fedux_org_stdlib/command/which'
4
+
5
+ module FeduxOrgStdlib
6
+ module Command
7
+ include RunCommand
8
+ include Which
9
+ end
10
+ end
@@ -0,0 +1,13 @@
1
+ # encoding: utf-8
2
+ module FeduxOrgStdlib
3
+ module Command
4
+ class CommandResult
5
+ attr_accessor :stderr, :stdout, :status
6
+
7
+ def initialize
8
+ @stderr = ''
9
+ @stdout = ''
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,55 @@
1
+ # encoding: utf-8
2
+ require 'fedux_org_stdlib/require_files'
3
+ require_library %w{ open3 }
4
+
5
+ require 'fedux_org_stdlib/environment'
6
+ require 'fedux_org_stdlib/command/command_result'
7
+
8
+ module FeduxOrgStdlib
9
+ module Command
10
+ module RunCommand
11
+ include Environment
12
+
13
+ # Execute command
14
+ #
15
+ # @param [String] cmd
16
+ # the command
17
+ #
18
+ # @param [Hash] options
19
+ # the options for command execution
20
+ #
21
+ # @option options [Hash] env ({])
22
+ # the environment variables for the command ('VAR' => 'CONTENT')
23
+ #
24
+ # @option options [String] stdin (nil)
25
+ # the string for stdin of the command
26
+ #
27
+ # @option options [TrueClass,FalseClass] binmode (false)
28
+ # should the stdin be read a binary or not
29
+ #
30
+ # @return [CommandResult]
31
+ # the result of the command execution
32
+ #
33
+ # @return [CommandResult]
34
+ # the result of the command execution
35
+ def run_command(cmd,options={})
36
+ opts = {
37
+ env: nil,
38
+ stdin: nil,
39
+ binmode: false,
40
+ working_directory: Dir.getwd,
41
+ }.merge options
42
+
43
+ env = opts[:env] || ENV.to_hash
44
+ stdin = opts[:stdin]
45
+ binmode = opts[:binmode]
46
+ working_directory = opts[:working_directory]
47
+
48
+ result = CommandResult.new
49
+ result.stdout, result.stderr, result.status = Open3.capture3(env, cmd, stdin_data: stdin, chdir: working_directory, binmode: binmode)
50
+
51
+ result
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,60 @@
1
+ # encoding: utf-8
2
+ require 'fedux_org_stdlib/require_files'
3
+ require_library %w{ pathname active_support/core_ext/object/blank }
4
+
5
+ module FeduxOrgStdlib
6
+ module Command
7
+ module Which
8
+
9
+ # Search for command
10
+ # @param [String] cmd
11
+ # name of command or path to command (will be reduced to basename and then searched in PATH)
12
+ #
13
+ # @param [Array,String] paths
14
+ # a string containing paths separated by "File::PATH_SEPARATOR" or an array of paths
15
+ #
16
+ # @param [Array,String] pathexts
17
+ # a string containing pathexts separated by ";" or an array of pathexts
18
+ #
19
+ # @return [String]
20
+ # path to command
21
+ def which(cmd, options={})
22
+ options = {
23
+ paths: ENV['PATH'].split(File::PATH_SEPARATOR),
24
+ pathexts: ENV['PATHEXT'].to_s.split( /;/ ),
25
+ raise_error_on_not_executable: false,
26
+ raise_error_on_not_found: false,
27
+ }.merge options
28
+
29
+ cmd = Pathname.new(cmd)
30
+
31
+ paths = options[:paths]
32
+ pathexts = options[:pathexts]
33
+ raise_error_on_not_executable = options[:raise_error_on_not_executable]
34
+ raise_error_on_not_found = options[:raise_error_on_not_found]
35
+
36
+ raise Exceptions::CommandNotFound if cmd.to_s.empty?
37
+ return nil if cmd.to_s.empty?
38
+
39
+ if cmd.absolute?
40
+ return cmd.to_s if cmd.executable?
41
+ raise Exceptions::CommandNotFound if raise_error_on_not_found and not cmd.exist?
42
+ raise Exceptions::CommandNotExecutable if raise_error_on_not_executable and not cmd.executable?
43
+ return nil
44
+ end
45
+
46
+ pathexts = [''] if pathexts.blank?
47
+
48
+ Array( paths ).each do |path|
49
+ Array( pathexts ).each do |ext|
50
+ file = Pathname.new( File.join(path, "#{cmd.to_s}#{ext.to_s}") )
51
+ return file.to_s if file.executable?
52
+ raise Exceptions::CommandNotExecutable if raise_error_on_not_executable and not cmd.executable?
53
+ end
54
+ end
55
+ raise Exceptions::CommandNotFound if raise_error_on_not_found
56
+ nil
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,6 @@
1
+ # encoding: utf-8
2
+ class Array
3
+ def to_list
4
+ self.map { |l| format('"%s"', l) }.join(", ")
5
+ end
6
+ end
@@ -0,0 +1,3 @@
1
+ # encoding: utf-8
2
+ class String
3
+ end
@@ -0,0 +1,32 @@
1
+ # encoding: utf-8
2
+ module FeduxOrgStdlib
3
+ module Environment
4
+
5
+ # Set environment variable for code block
6
+ #
7
+ # @param [Hash] new_environment_variables
8
+ # the variables which should be set for that environment
9
+ #
10
+ # @param [Hash] options
11
+ # options for environment manipulation
12
+ #
13
+ # @option options [True,False] :clear
14
+ # Should the environment clear before merge?
15
+ #
16
+ # @yield
17
+ # the block which should be run which the change environment
18
+ def isolated_environment(new_environment_variables, options={}, &block)
19
+ old_environment = ENV.to_hash
20
+
21
+ ENV.clear if options[:clear] == true
22
+ ENV.update new_environment_variables
23
+
24
+ block.call
25
+ ensure
26
+ ENV.clear
27
+ ENV.update old_environment
28
+ end
29
+
30
+ alias_method :with_environment, :isolated_environment
31
+ end
32
+ end
@@ -0,0 +1,248 @@
1
+ # encoding: utf-8
2
+ require 'fedux_org_stdlib/require_files'
3
+ require_library %w{ fileutils }
4
+
5
+ require 'fedux_org_stdlib/filesystem/exceptions'
6
+
7
+ # helper for file system
8
+ module FeduxOrgStdlib
9
+ module Filesystem
10
+
11
+ # The root directory of the project
12
+ #
13
+ # @return [String]
14
+ # the root directory
15
+ def root_directory
16
+ #::File.expand_path('../../../', __FILE__)
17
+ raise Exceptions::InvalidUsageOfLibrary , "Sorry, but you need to define the root directory yourself"
18
+ end
19
+
20
+ # The temporary directory for the project
21
+ #
22
+ # @return [String]
23
+ # the directory created for the tests
24
+ def working_directory
25
+ ::File.join(root_directory, 'tmp', 'test')
26
+ end
27
+
28
+ # Create temporary directory
29
+ def create_working_directory
30
+ FileUtils.mkdir_p(working_directory) unless ::File.exists? working_directory
31
+ end
32
+
33
+ # Delete temporary directory
34
+ def delete_working_directory
35
+ FileUtils.rm_rf(working_directory) if ::File.exists? working_directory
36
+ end
37
+
38
+ # Clean up test directory
39
+ def cleanup_working_directory
40
+ delete_working_directory
41
+ create_working_directory
42
+ end
43
+
44
+ # Switch the current working directory to
45
+ # the temporary one and execute code block
46
+ def switch_to_working_directory(&block)
47
+ Dir.chdir(working_directory, &block)
48
+ end
49
+ alias_method :in_working_directory, :switch_to_working_directory
50
+
51
+ # Create directory(ies)
52
+ #
53
+ # @param [String,Array] dirs
54
+ # the directories to be created, multiple arguments are possible as well
55
+ #
56
+ # @return [String,Array]
57
+ # returns a string if there was only one file given, and an array with
58
+ # muliple files
59
+ def create_directory(*dirs)
60
+ raise_if_forbidden_path_for_create_operation(dirs)
61
+
62
+ directories = expand_path(dirs.flatten)
63
+ FileUtils.mkdir_p(directories)
64
+
65
+ if directories.size == 1
66
+ return directories.first
67
+ else
68
+ return directories
69
+ end
70
+ end
71
+
72
+ # Delete directory(ies)
73
+ #
74
+ # @param [String, Array] dirs
75
+ # the directories to be deleted, multiple arguments are possible as well
76
+ #
77
+ # @return [String,Array]
78
+ # returns a string if there was only one file given, and an array with
79
+ # muliple files
80
+ def delete_directory(*dirs)
81
+ raise_if_forbidden_path_for_delete_operation(dirs)
82
+
83
+ directories = expand_path(dirs.flatten)
84
+ FileUtils.rm_r(directories)
85
+
86
+ if directories.size == 1
87
+ return directories.first
88
+ else
89
+ return directories
90
+ end
91
+ end
92
+
93
+ # Check existence of path(s)
94
+ #
95
+ # @param [String,Array] paths
96
+ # which path(s) should be checked, multiple arguments are possible as well
97
+ #
98
+ # @return [TrueClass,FalseClass]
99
+ # the result of all checks done
100
+ def path_exists?(*paths)
101
+ raise_if_forbidden_path_for_create_operation(paths)
102
+
103
+ paths_expanded = expand_path(paths.flatten)
104
+ paths_expanded.flatten.all? { |p| ::File.exists?(p) }
105
+ end
106
+
107
+ # Check absence of path(s)
108
+ #
109
+ # @param [String,Array] paths
110
+ # which path(s) should be checked, multiple arguments are possible as well
111
+ #
112
+ # @return [TrueClass,FalseClass]
113
+ # the result of all checks done
114
+ def path_does_not_exist?(*paths)
115
+ not path_exists?(paths)
116
+ end
117
+
118
+ # Create a single file
119
+ #
120
+ # @param [String] path
121
+ # the path for the new file (can include directories)
122
+ #
123
+ # @param [String] content
124
+ # the content written to the file
125
+ #
126
+ # @return [String]
127
+ # the path to the created file
128
+ def create_file(path, content='', mode=0644)
129
+ raise_if_forbidden_path_for_create_operation(path)
130
+
131
+ file = expand_path(path).first
132
+ directory = ::File.dirname(file)
133
+
134
+ FileUtils.mkdir_p(directory) unless directory == '.'
135
+ ::File.open(file, "wb") do |f|
136
+ f.write content
137
+ end
138
+
139
+ FileUtils.chmod(mode, file)
140
+
141
+ file
142
+ end
143
+
144
+ # Delete a single file
145
+ #
146
+ # @param [String] files
147
+ # the path for the new file (can include directories)
148
+ #
149
+ # @return [String]
150
+ # the path to the deleted file
151
+ def delete_file(*files)
152
+ raise_if_forbidden_path_for_delete_operation(files)
153
+
154
+ files_to_be_deleted = expand_path(files.flatten)
155
+ FileUtils.rm(files_to_be_deleted)
156
+
157
+ if files_to_be_deleted.size == 1
158
+ return files_to_be_deleted.first
159
+ else
160
+ return files_to_be_deleted
161
+ end
162
+ end
163
+
164
+ # Read the content of a file
165
+ #
166
+ # @param [String] path
167
+ # the path to the file
168
+ #
169
+ # @return [String,Binary]
170
+ # the content of the file
171
+ def read_file(path)
172
+ raise_if_forbidden_path_for_create_operation(path)
173
+
174
+ file_path = expand_path(path).first
175
+ return ::File.read(file_path)
176
+ end
177
+
178
+ # Expand path based on temporary directory
179
+ #
180
+ # @param [String, Array, Multiple Values] paths
181
+ # the paths to be expanded
182
+ #
183
+ # @return [Array, String]
184
+ # the expanded arrays
185
+ def expand_path(*paths)
186
+ raise_if_forbidden_path_for_create_operation(paths)
187
+
188
+ paths.flatten.map do |p|
189
+ case p
190
+ when /^~/
191
+ ::File.expand_path(p)
192
+ else
193
+ ::File.join(working_directory, p )
194
+ end
195
+ end
196
+ end
197
+
198
+ # Check if path is forbidden for delete operation
199
+ #
200
+ # @param [String, Array] paths
201
+ # paths which should be checked
202
+ #
203
+ # @return [TrueClass, FalseClass]
204
+ # true if path is forbidden, false if path is not forbidden
205
+ def raise_if_forbidden_path_for_create_operation(*paths)
206
+ flattend_paths = paths.flatten
207
+ strings = []
208
+ regex = %r[\.\.]
209
+
210
+ raise FileSystem::Exceptions::InvalidPath , "Sorry, but you cannot use paths matching \"#{strings.join(', ')}\" or \"#{regex.inspect}\" here!" if path_matches?(strings, regex, flattend_paths)
211
+ end
212
+
213
+ # Check if path is forbidden for delete operation
214
+ #
215
+ # @param [String, Array] paths
216
+ # paths which should be checked
217
+ #
218
+ # @return [TrueClass, FalseClass]
219
+ # true if path is forbidden, false if path is not forbidden
220
+ def raise_if_forbidden_path_for_delete_operation(*paths)
221
+ flattend_paths = paths.flatten
222
+ strings = %w[ / ]
223
+ regex = %r[\.\.]
224
+
225
+ raise FileSystem::Exceptions::InvalidPath , "Sorry, but you cannot use paths matching \"#{strings.join(', ')}\" or \"#{regex.to_s}\" here!" if path_matches?(strings, regex, flattend_paths)
226
+ end
227
+
228
+ # Check if path matches
229
+ #
230
+ # @param [Array, String] strings
231
+ # strings which are checked against paths
232
+ #
233
+ # @param [Array, String] regex
234
+ # regex which is checked against path
235
+ #
236
+ # @param [Array, String] paths
237
+ # the paths to be checked
238
+ #
239
+ # @return [TrueClass, FalseClass]
240
+ # true if path is valid, false if invalid
241
+ def path_matches?(strings, regex, *paths)
242
+ flattend_paths = paths.flatten
243
+ flattend_strings = strings.flatten
244
+
245
+ flattend_paths.any? { |f| f =~ regex or flattend_strings.include?(f) }
246
+ end
247
+ end
248
+ end