avm-files 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/avm/files/appendable/file_content.rb +24 -0
- data/lib/avm/files/appendable/plain_directory.rb +25 -0
- data/lib/avm/files/appendable/resource_base.rb +13 -0
- data/lib/avm/files/appendable/tar_output_command.rb +26 -0
- data/lib/avm/files/appendable/templatized_directory.rb +29 -0
- data/lib/avm/files/appendable.rb +55 -0
- data/lib/avm/files/appender.rb +11 -0
- data/lib/avm/files/deploy.rb +71 -0
- data/lib/avm/files/formatter/formats/base.rb +62 -0
- data/lib/avm/files/formatter/formats/generic_plain.rb +34 -0
- data/lib/avm/files/formatter/formats/html.rb +45 -0
- data/lib/avm/files/formatter/formats/javascript.rb +24 -0
- data/lib/avm/files/formatter/formats/json.rb +27 -0
- data/lib/avm/files/formatter/formats/php.rb +22 -0
- data/lib/avm/files/formatter/formats/python.rb +22 -0
- data/lib/avm/files/formatter/formats/ruby.rb +22 -0
- data/lib/avm/files/formatter/formats/xml.rb +28 -0
- data/lib/avm/files/formatter/formats.rb +13 -0
- data/lib/avm/files/formatter/utf8_assert.rb +74 -0
- data/lib/avm/files/formatter.rb +90 -0
- data/lib/avm/files/rotate.rb +107 -0
- data/lib/avm/files/version.rb +7 -0
- data/lib/avm/files.rb +9 -0
- metadata +134 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 1ae0d20f3f31743dfbf25f94dd24f36dc97dce93da94377fc9f85742b10d3e34
|
4
|
+
data.tar.gz: 23ed109ef8b2691f7b14fdaeaab8e194d4ff8a80739130c043aa109988f57e98
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: '085fd89600d3d0cdda2f4cc731eb14380913c578d24d75c2e82acafe690d7600111087622a6e94aad5707dfa0c8f3bb3735f64ad4a364ca356d49390cf590de0'
|
7
|
+
data.tar.gz: b04360ba65b34e9e0678273127b74e1ae5aef1b0a55844ce965c55407e86695ac80b42d395586b410778eb766bceb211f6d59c22df4b89a94d31535c82736dad
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'eac_ruby_utils/core_ext'
|
4
|
+
require 'avm/files/appendable/resource_base'
|
5
|
+
|
6
|
+
module Avm
|
7
|
+
module Files
|
8
|
+
module Appendable
|
9
|
+
class FileContent < ::Avm::Files::Appendable::ResourceBase
|
10
|
+
attr_reader :target_path, :content
|
11
|
+
|
12
|
+
def initialize(deploy, target_path, content)
|
13
|
+
super(deploy)
|
14
|
+
@target_path = target_path
|
15
|
+
@content = content
|
16
|
+
end
|
17
|
+
|
18
|
+
def write_on(target_dir)
|
19
|
+
target_dir.join(target_path).write(content)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'eac_ruby_utils/core_ext'
|
4
|
+
require 'avm/files/appendable/resource_base'
|
5
|
+
|
6
|
+
module Avm
|
7
|
+
module Files
|
8
|
+
module Appendable
|
9
|
+
class PlainDirectory < ::Avm::Files::Appendable::ResourceBase
|
10
|
+
attr_reader :source_path
|
11
|
+
|
12
|
+
def initialize(appender, source_path)
|
13
|
+
super(appender)
|
14
|
+
@source_path = source_path.to_pathname
|
15
|
+
end
|
16
|
+
|
17
|
+
def write_on(target_dir)
|
18
|
+
raise "\"#{source_path}\" is not a directory" unless source_path.directory?
|
19
|
+
|
20
|
+
::FileUtils.cp_r("#{source_path}/.", target_dir.to_path)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'eac_ruby_utils/core_ext'
|
4
|
+
require 'eac_ruby_utils/envs'
|
5
|
+
require 'avm/files/appendable/resource_base'
|
6
|
+
|
7
|
+
module Avm
|
8
|
+
module Files
|
9
|
+
module Appendable
|
10
|
+
class TarOutputCommand < ::Avm::Files::Appendable::ResourceBase
|
11
|
+
attr_reader :command
|
12
|
+
|
13
|
+
def initialize(appender, command)
|
14
|
+
super(appender)
|
15
|
+
@command = command
|
16
|
+
end
|
17
|
+
|
18
|
+
def write_on(target_dir)
|
19
|
+
command.pipe(
|
20
|
+
::EacRubyUtils::Envs.local.command('tar', '-xf', '-', '-C', target_dir)
|
21
|
+
).execute!
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'eac_ruby_utils/core_ext'
|
4
|
+
require 'avm/files/appendable/resource_base'
|
5
|
+
require 'eac_templates/directory'
|
6
|
+
|
7
|
+
module Avm
|
8
|
+
module Files
|
9
|
+
module Appendable
|
10
|
+
class TemplatizedDirectory < ::Avm::Files::Appendable::ResourceBase
|
11
|
+
attr_reader :source_path
|
12
|
+
|
13
|
+
def initialize(appender, source_path)
|
14
|
+
super(appender)
|
15
|
+
@source_path = source_path
|
16
|
+
end
|
17
|
+
|
18
|
+
def write_on(target_dir)
|
19
|
+
raise 'Variables source not set' if appender.variables_source.blank?
|
20
|
+
|
21
|
+
::EacTemplates::Directory.new(source_path).apply(
|
22
|
+
appender.variables_source,
|
23
|
+
target_dir
|
24
|
+
)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'eac_ruby_utils/core_ext'
|
4
|
+
|
5
|
+
module Avm
|
6
|
+
module Files
|
7
|
+
module Appendable
|
8
|
+
require_sub __FILE__
|
9
|
+
|
10
|
+
attr_reader :variables_source
|
11
|
+
|
12
|
+
def appended
|
13
|
+
@appended ||= []
|
14
|
+
end
|
15
|
+
|
16
|
+
def append_templatized_directories(directories)
|
17
|
+
directories.each { |directory| append_templatized_directory(directory) }
|
18
|
+
self
|
19
|
+
end
|
20
|
+
|
21
|
+
def append_plain_directory(directory)
|
22
|
+
appended << ::Avm::Files::Appendable::PlainDirectory.new(self, directory)
|
23
|
+
self
|
24
|
+
end
|
25
|
+
|
26
|
+
def append_tar_output_command(tar_command)
|
27
|
+
appended << ::Avm::Files::Appendable::TarOutputCommand.new(self, tar_command)
|
28
|
+
self
|
29
|
+
end
|
30
|
+
|
31
|
+
def append_templatized_directory(directory)
|
32
|
+
appended << ::Avm::Files::Appendable::TemplatizedDirectory.new(self, directory)
|
33
|
+
self
|
34
|
+
end
|
35
|
+
|
36
|
+
def append_file_content(target_path, content)
|
37
|
+
appended << ::Avm::Files::Appendable::FileContent
|
38
|
+
.new(self, target_path, content)
|
39
|
+
self
|
40
|
+
end
|
41
|
+
|
42
|
+
def variables_source_set(source)
|
43
|
+
@variables_source = source
|
44
|
+
self
|
45
|
+
end
|
46
|
+
|
47
|
+
def write_appended_on(target_dir)
|
48
|
+
target_dir = target_dir.to_pathname
|
49
|
+
raise "\"#{target_dir}\" is not a directory" unless target_dir.directory?
|
50
|
+
|
51
|
+
appended.each { |append| append.write_on(target_dir) }
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'addressable'
|
4
|
+
require 'avm/files/appendable'
|
5
|
+
require 'eac_ruby_utils/core_ext'
|
6
|
+
|
7
|
+
module Avm
|
8
|
+
module Files
|
9
|
+
class Deploy
|
10
|
+
include ::Avm::Files::Appendable
|
11
|
+
enable_simple_cache
|
12
|
+
|
13
|
+
attr_reader :build_dir, :target_env, :target_path
|
14
|
+
|
15
|
+
common_constructor :target_env, :target_path
|
16
|
+
|
17
|
+
def run
|
18
|
+
on_build_dir do
|
19
|
+
copy_content_to_build_dir
|
20
|
+
mkdir_target
|
21
|
+
clear_content
|
22
|
+
send_untar_package
|
23
|
+
set_target_permission
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def on_build_dir
|
30
|
+
@build_dir = ::Dir.mktmpdir
|
31
|
+
yield
|
32
|
+
ensure
|
33
|
+
::FileUtils.rm_rf(@build_dir)
|
34
|
+
end
|
35
|
+
|
36
|
+
def copy_content_to_build_dir
|
37
|
+
write_appended_on(build_dir)
|
38
|
+
end
|
39
|
+
|
40
|
+
def mkdir_target
|
41
|
+
target_env.command('mkdir', '-p', target_path).execute!
|
42
|
+
end
|
43
|
+
|
44
|
+
def clear_content
|
45
|
+
target_env.command(
|
46
|
+
'find', target_path, '-mindepth', '1', '-maxdepth', '1', '-exec', 'rm', '-rf', '{}', ';'
|
47
|
+
).execute!
|
48
|
+
end
|
49
|
+
|
50
|
+
def send_untar_package
|
51
|
+
tar_build_command.pipe(untar_build_command).execute!
|
52
|
+
end
|
53
|
+
|
54
|
+
def set_target_permission
|
55
|
+
target_env.command('chmod', '755', target_path).execute!
|
56
|
+
end
|
57
|
+
|
58
|
+
def tar_build_command
|
59
|
+
source_env.command('tar', '-czO', '-C', build_dir, '.')
|
60
|
+
end
|
61
|
+
|
62
|
+
def untar_build_command
|
63
|
+
target_env.command('tar', '-xzf', '-', '-C', target_path)
|
64
|
+
end
|
65
|
+
|
66
|
+
def source_env
|
67
|
+
::EacRubyUtils::Envs.local
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'eac_fs/file_info'
|
4
|
+
require 'ostruct'
|
5
|
+
|
6
|
+
module Avm
|
7
|
+
module Files
|
8
|
+
class Formatter
|
9
|
+
module Formats
|
10
|
+
class Base
|
11
|
+
def apply(files)
|
12
|
+
old_content = Hash[files.map { |f| [f, File.read(f)] }]
|
13
|
+
::Avm::Files::Formatter::Utf8Assert.assert_files(files) { internal_apply(files) }
|
14
|
+
files.map { |f| build_file_result(f, old_content[f]) }
|
15
|
+
end
|
16
|
+
|
17
|
+
def name
|
18
|
+
self.class.name.demodulize
|
19
|
+
end
|
20
|
+
|
21
|
+
def match?(file)
|
22
|
+
match_by_filename?(file) || match_by_type?(file)
|
23
|
+
end
|
24
|
+
|
25
|
+
def valid_basenames
|
26
|
+
constant_or_array('VALID_BASENAMES')
|
27
|
+
end
|
28
|
+
|
29
|
+
def valid_types
|
30
|
+
constant_or_array('VALID_TYPES')
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def constant_or_array(name)
|
36
|
+
return [] unless self.class.const_defined?(name)
|
37
|
+
|
38
|
+
self.class.const_get(name)
|
39
|
+
end
|
40
|
+
|
41
|
+
def build_file_result(file, old_content)
|
42
|
+
::OpenStruct.new(file: file, format: self.class,
|
43
|
+
changed: (old_content != File.read(file)))
|
44
|
+
end
|
45
|
+
|
46
|
+
def match_by_filename?(file)
|
47
|
+
valid_basenames.any? do |valid_basename|
|
48
|
+
file.basename.fnmatch?(valid_basename)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def match_by_type?(file)
|
53
|
+
info = ::EacFs::FileInfo.new(file)
|
54
|
+
return unless info.content_type.type == 'text'
|
55
|
+
|
56
|
+
valid_types.include?(info.content_type.subtype)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'avm/files/formatter/formats/base'
|
4
|
+
|
5
|
+
module Avm
|
6
|
+
module Files
|
7
|
+
class Formatter
|
8
|
+
module Formats
|
9
|
+
class GenericPlain < ::Avm::Files::Formatter::Formats::Base
|
10
|
+
VALID_BASENAMES = %w[*.bat *.css.coffee *.java *.js *.rb *.scss *.sql *.tex *.url *.yml
|
11
|
+
*.yaml].freeze
|
12
|
+
|
13
|
+
VALID_TYPES = %w[plain x-shellscript].freeze
|
14
|
+
|
15
|
+
def internal_apply(files)
|
16
|
+
files.each { |file| file_apply(file) }
|
17
|
+
end
|
18
|
+
|
19
|
+
def file_apply(file)
|
20
|
+
file.write(string_apply(file.read))
|
21
|
+
end
|
22
|
+
|
23
|
+
def string_apply(string)
|
24
|
+
b = ''
|
25
|
+
string.each_line do |line|
|
26
|
+
b += "#{line.rstrip}\n"
|
27
|
+
end
|
28
|
+
"#{b.strip}\n".gsub(/\t/, ' ')
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'avm/files/formatter/formats/generic_plain'
|
4
|
+
require 'htmlbeautifier'
|
5
|
+
|
6
|
+
module Avm
|
7
|
+
module Files
|
8
|
+
class Formatter
|
9
|
+
module Formats
|
10
|
+
class Html < ::Avm::Files::Formatter::Formats::GenericPlain
|
11
|
+
VALID_BASENAMES = %w[*.html *.html.erb].freeze
|
12
|
+
VALID_TYPES = ['html'].freeze
|
13
|
+
|
14
|
+
def file_apply(path)
|
15
|
+
input = ::File.read(path)
|
16
|
+
temppath = tempfile_path
|
17
|
+
::File.open(temppath, 'w') do |output|
|
18
|
+
beautify path, input, output
|
19
|
+
end
|
20
|
+
::FileUtils.mv(temppath, path)
|
21
|
+
super(path)
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def beautify(name, input, output)
|
27
|
+
output.puts ::HtmlBeautifier.beautify(input, htmlbeautify_options)
|
28
|
+
rescue StandardError => e
|
29
|
+
raise "Error parsing #{name}: #{e}"
|
30
|
+
end
|
31
|
+
|
32
|
+
def htmlbeautify_options
|
33
|
+
@htmlbeautify_options ||= { indent: ' ' }
|
34
|
+
end
|
35
|
+
|
36
|
+
def tempfile_path
|
37
|
+
tempfile = ::Tempfile.new
|
38
|
+
tempfile.close
|
39
|
+
tempfile.path
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'avm/executables'
|
4
|
+
require 'avm/files/formatter/formats/generic_plain'
|
5
|
+
|
6
|
+
module Avm
|
7
|
+
module Files
|
8
|
+
class Formatter
|
9
|
+
module Formats
|
10
|
+
class Javascript < ::Avm::Files::Formatter::Formats::GenericPlain
|
11
|
+
VALID_BASENAMES = %w[*.js].freeze
|
12
|
+
VALID_TYPES = [].freeze
|
13
|
+
|
14
|
+
def internal_apply(files)
|
15
|
+
::Avm::Executables.js_beautify.command.append(
|
16
|
+
['--indent-size=2', '--end-with-newline', '--replace', *files]
|
17
|
+
).system!
|
18
|
+
super(files)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'avm/files/formatter/formats/generic_plain'
|
4
|
+
|
5
|
+
module Avm
|
6
|
+
module Files
|
7
|
+
class Formatter
|
8
|
+
module Formats
|
9
|
+
class Json < ::Avm::Files::Formatter::Formats::GenericPlain
|
10
|
+
VALID_BASENAMES = %w[*.json].freeze
|
11
|
+
VALID_TYPES = [].freeze
|
12
|
+
|
13
|
+
def file_apply(file)
|
14
|
+
::File.write(file, ::JSON.pretty_generate(::JSON.parse(::File.read(file))))
|
15
|
+
end
|
16
|
+
|
17
|
+
def json_file?(file)
|
18
|
+
::JSON.parse(::File.read(file))
|
19
|
+
true
|
20
|
+
rescue JSON::ParserError
|
21
|
+
false
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'avm/executables'
|
4
|
+
require 'avm/files/formatter/formats/generic_plain'
|
5
|
+
|
6
|
+
module Avm
|
7
|
+
module Files
|
8
|
+
class Formatter
|
9
|
+
module Formats
|
10
|
+
class Php < ::Avm::Files::Formatter::Formats::GenericPlain
|
11
|
+
VALID_BASENAMES = %w[*.php].freeze
|
12
|
+
VALID_TYPES = ['x-php'].freeze
|
13
|
+
|
14
|
+
def file_apply(file)
|
15
|
+
::Avm::Executables.php_cs_fixer.command.append(['fix', file]).system!
|
16
|
+
super(file)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'avm/executables'
|
4
|
+
require 'avm/files/formatter/formats/generic_plain'
|
5
|
+
|
6
|
+
module Avm
|
7
|
+
module Files
|
8
|
+
class Formatter
|
9
|
+
module Formats
|
10
|
+
class Python < ::Avm::Files::Formatter::Formats::GenericPlain
|
11
|
+
VALID_BASENAMES = %w[*.py].freeze
|
12
|
+
VALID_TYPES = ['x-python'].freeze
|
13
|
+
|
14
|
+
def internal_apply(files)
|
15
|
+
::Avm::Executables.yapf.command.append(['--in-place', *files]).system!
|
16
|
+
super(files)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'avm/files/formatter/formats/generic_plain'
|
4
|
+
require 'avm/ruby/rubocop'
|
5
|
+
|
6
|
+
module Avm
|
7
|
+
module Files
|
8
|
+
class Formatter
|
9
|
+
module Formats
|
10
|
+
class Ruby < ::Avm::Files::Formatter::Formats::GenericPlain
|
11
|
+
VALID_BASENAMES = %w[*.gemspec *.rake *.rb Gemfile Rakefile].freeze
|
12
|
+
VALID_TYPES = ['x-ruby'].freeze
|
13
|
+
|
14
|
+
def internal_apply(files)
|
15
|
+
::Avm::Ruby::Rubocop.new('.', ['-a', '--ignore-parent-exclusion'] + files).run
|
16
|
+
super(files)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'avm/executables'
|
4
|
+
require 'avm/files/formatter/formats/generic_plain'
|
5
|
+
|
6
|
+
module Avm
|
7
|
+
module Files
|
8
|
+
class Formatter
|
9
|
+
module Formats
|
10
|
+
class Xml < ::Avm::Files::Formatter::Formats::GenericPlain
|
11
|
+
VALID_BASENAMES = %w[*.xml].freeze
|
12
|
+
VALID_TYPES = ['xml'].freeze
|
13
|
+
|
14
|
+
def internal_apply(files)
|
15
|
+
format_command(files).system!
|
16
|
+
super(files)
|
17
|
+
end
|
18
|
+
|
19
|
+
def format_command(files)
|
20
|
+
::Avm::Executables.tidy.command.append(
|
21
|
+
%w[-xml -modify --indent auto --indent-spaces 2 --wrap 100] + files
|
22
|
+
)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'eac_fs/file_info'
|
4
|
+
|
5
|
+
module Avm
|
6
|
+
module Files
|
7
|
+
class Formatter
|
8
|
+
class Utf8Assert
|
9
|
+
UTF8_CHARSET = 'utf-8'
|
10
|
+
UTF8_CHARSETS = [UTF8_CHARSET, 'us-ascii'].freeze
|
11
|
+
|
12
|
+
class << self
|
13
|
+
def assert_files(files)
|
14
|
+
asserters = files.map { |file| new(file) }
|
15
|
+
begin
|
16
|
+
asserters.each(&:assert)
|
17
|
+
yield
|
18
|
+
ensure
|
19
|
+
asserters.each(&:revert)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
enable_simple_cache
|
25
|
+
common_constructor :path
|
26
|
+
|
27
|
+
def assert
|
28
|
+
return if original_utf8?
|
29
|
+
|
30
|
+
convert_self(original_charset, UTF8_CHARSET)
|
31
|
+
end
|
32
|
+
|
33
|
+
def revert
|
34
|
+
return if original_utf8?
|
35
|
+
|
36
|
+
convert_self(UTF8_CHARSET, original_charset)
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def original_info_uncached
|
42
|
+
::EacFs::FileInfo.new(path)
|
43
|
+
end
|
44
|
+
|
45
|
+
def original_charset_uncached
|
46
|
+
return original_info.content_type.charset if original_info.content_type.charset.present?
|
47
|
+
|
48
|
+
raise 'No charset found'
|
49
|
+
rescue StandardError => e
|
50
|
+
raise "Unable to determine the charset of #{path} (#{e.message})"
|
51
|
+
end
|
52
|
+
|
53
|
+
def original_utf8?
|
54
|
+
UTF8_CHARSETS.include?(original_charset)
|
55
|
+
end
|
56
|
+
|
57
|
+
def convert_file(from_path, from_charset, to_path, to_charset)
|
58
|
+
File.open(from_path, "r:#{from_charset}") do |input|
|
59
|
+
File.open(to_path, "w:#{to_charset}") do |output|
|
60
|
+
output.write(input.read)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def convert_self(from_charset, to_charset)
|
66
|
+
temp = ::Tempfile.new
|
67
|
+
temp.close
|
68
|
+
convert_file(path, from_charset, temp.path, to_charset)
|
69
|
+
::FileUtils.mv(temp.path, path)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'eac_ruby_utils/core_ext'
|
4
|
+
require 'eac_ruby_utils/fs/traversable'
|
5
|
+
|
6
|
+
module Avm
|
7
|
+
module Files
|
8
|
+
class Formatter
|
9
|
+
include ::EacRubyUtils::Fs::Traversable
|
10
|
+
require_sub __FILE__
|
11
|
+
enable_simple_cache
|
12
|
+
enable_speaker
|
13
|
+
enable_listable
|
14
|
+
lists.add_symbol :option, :apply, :recursive, :verbose
|
15
|
+
common_constructor :source_paths, :options, default: [{}] do
|
16
|
+
options.assert_valid_keys(self.class.lists.option.values)
|
17
|
+
end
|
18
|
+
|
19
|
+
FORMATS = %w[ruby php html python xml javascript json generic_plain].freeze
|
20
|
+
|
21
|
+
def run
|
22
|
+
clear
|
23
|
+
search_files
|
24
|
+
apply
|
25
|
+
show_results
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def apply
|
31
|
+
infom "Applying #{@formats_files.count} format(s)... "
|
32
|
+
@formats_files.each do |format, files|
|
33
|
+
infom "Applying format #{format.name} (Files matched: #{files.count})..."
|
34
|
+
next unless options[OPTION_APPLY]
|
35
|
+
|
36
|
+
@result += format.apply(files)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def traverser_check_file(file)
|
41
|
+
format = find_format(file)
|
42
|
+
infov file, format ? format.class : '-' if options[OPTION_VERBOSE]
|
43
|
+
return unless format
|
44
|
+
|
45
|
+
@formats_files[format] ||= []
|
46
|
+
@formats_files[format] << file
|
47
|
+
end
|
48
|
+
|
49
|
+
def clear
|
50
|
+
@formats_files = {}
|
51
|
+
@result = []
|
52
|
+
end
|
53
|
+
|
54
|
+
def find_format(file)
|
55
|
+
formats.each do |c|
|
56
|
+
return c if c.match?(file)
|
57
|
+
end
|
58
|
+
nil
|
59
|
+
end
|
60
|
+
|
61
|
+
def formats_uncached
|
62
|
+
FORMATS.map do |identifier|
|
63
|
+
"avm/files/formatter/formats/#{identifier}".camelize.constantize.new
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def search_files
|
68
|
+
infov 'Directories to search', source_paths.count
|
69
|
+
source_paths.each do |source_path|
|
70
|
+
infom "Searching files on \"#{source_path}\"..."
|
71
|
+
traverser_check_path(source_path)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def show_results
|
76
|
+
changed = @result.select(&:changed)
|
77
|
+
changed.each do |h|
|
78
|
+
out h.file.to_s.cyan
|
79
|
+
out " (#{h.format})".yellow
|
80
|
+
puts ' changed'.green
|
81
|
+
end
|
82
|
+
infov('Files changed', "#{changed.count}/#{@result.count}")
|
83
|
+
end
|
84
|
+
|
85
|
+
def traverser_recursive
|
86
|
+
options[OPTION_RECURSIVE]
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
@@ -0,0 +1,107 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_support/core_ext/object/blank'
|
4
|
+
require 'eac_ruby_utils/simple_cache'
|
5
|
+
|
6
|
+
module Avm
|
7
|
+
module Files
|
8
|
+
class Rotate
|
9
|
+
include ::EacRubyUtils::SimpleCache
|
10
|
+
|
11
|
+
attr_reader :options, :source_path
|
12
|
+
|
13
|
+
def initialize(source_path, options = {})
|
14
|
+
@source_path = source_path
|
15
|
+
@options = options
|
16
|
+
end
|
17
|
+
|
18
|
+
def run
|
19
|
+
validate_msg = validate
|
20
|
+
return validate_msg if validate_msg.present?
|
21
|
+
|
22
|
+
::FileUtils.mv(source_path, target_path)
|
23
|
+
check_space_limit
|
24
|
+
nil
|
25
|
+
end
|
26
|
+
|
27
|
+
def space_limit
|
28
|
+
r = options[:space_limit].try(:to_i)
|
29
|
+
return r if r.present? && r.positive?
|
30
|
+
|
31
|
+
r
|
32
|
+
end
|
33
|
+
|
34
|
+
def space_used
|
35
|
+
rotated_files.inject(0) { |a, e| a + ::File.size(e) }
|
36
|
+
end
|
37
|
+
|
38
|
+
def rotated_files
|
39
|
+
::Dir["#{dirname}/#{source_basename_without_extension}*#{source_extension}"]
|
40
|
+
end
|
41
|
+
|
42
|
+
def oldest_rotated_file
|
43
|
+
rotated_files.min_by { |file| [::File.mtime(file)] }
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
def check_space_limit
|
49
|
+
return unless space_limit
|
50
|
+
|
51
|
+
while space_used > space_limit
|
52
|
+
file = oldest_rotated_file
|
53
|
+
unless file
|
54
|
+
raise 'oldest_rotated_file returned nil ' \
|
55
|
+
"(Limit: #{space_limit}, used: #{space_used})"
|
56
|
+
end
|
57
|
+
::File.delete(file)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def validate
|
62
|
+
return "Source file \"#{source_path}\" does not exist" unless ::File.exist?(source_path)
|
63
|
+
return "File \"#{source_path}\" is already rotated" if source_rotated?
|
64
|
+
|
65
|
+
nil
|
66
|
+
end
|
67
|
+
|
68
|
+
def source_rotated?
|
69
|
+
source_basename_without_extension.match(/[PN]\d{4}\z/)
|
70
|
+
end
|
71
|
+
|
72
|
+
def source_extension_uncached
|
73
|
+
file_extension(::File.basename(source_path))
|
74
|
+
end
|
75
|
+
|
76
|
+
def source_basename_without_extension
|
77
|
+
::File.basename(source_path, source_extension)
|
78
|
+
end
|
79
|
+
|
80
|
+
def target_path_uncached
|
81
|
+
::File.join(dirname, target_basename)
|
82
|
+
end
|
83
|
+
|
84
|
+
def dirname
|
85
|
+
::File.dirname(source_path)
|
86
|
+
end
|
87
|
+
|
88
|
+
def target_basename
|
89
|
+
source_basename_without_extension + target_suffix + source_extension
|
90
|
+
end
|
91
|
+
|
92
|
+
def target_suffix
|
93
|
+
return '_UNKNOWN_MTIME' unless ::File.exist?(source_path)
|
94
|
+
|
95
|
+
t = ::File.mtime(source_path)
|
96
|
+
t.strftime('_%Y-%m-%d_%H-%M-%S_') + t.strftime('%z').gsub(/\A\+/, 'P').gsub(/\A\-/, 'N')
|
97
|
+
end
|
98
|
+
|
99
|
+
def file_extension(basename)
|
100
|
+
extension = ::File.extname(basename)
|
101
|
+
return '' if extension.blank?
|
102
|
+
|
103
|
+
file_extension(::File.basename(basename, extension)) + extension
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
data/lib/avm/files.rb
ADDED
metadata
ADDED
@@ -0,0 +1,134 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: avm-files
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Put here the authors
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-10-13 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: avm
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.6'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: eac_fs
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0.5'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0.5'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: eac_ruby_utils
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0.76'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0.76'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: eac_templates
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.2'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.2'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: eac_ruby_gem_support
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0.4'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0.4'
|
83
|
+
description:
|
84
|
+
email:
|
85
|
+
executables: []
|
86
|
+
extensions: []
|
87
|
+
extra_rdoc_files: []
|
88
|
+
files:
|
89
|
+
- lib/avm/files.rb
|
90
|
+
- lib/avm/files/appendable.rb
|
91
|
+
- lib/avm/files/appendable/file_content.rb
|
92
|
+
- lib/avm/files/appendable/plain_directory.rb
|
93
|
+
- lib/avm/files/appendable/resource_base.rb
|
94
|
+
- lib/avm/files/appendable/tar_output_command.rb
|
95
|
+
- lib/avm/files/appendable/templatized_directory.rb
|
96
|
+
- lib/avm/files/appender.rb
|
97
|
+
- lib/avm/files/deploy.rb
|
98
|
+
- lib/avm/files/formatter.rb
|
99
|
+
- lib/avm/files/formatter/formats.rb
|
100
|
+
- lib/avm/files/formatter/formats/base.rb
|
101
|
+
- lib/avm/files/formatter/formats/generic_plain.rb
|
102
|
+
- lib/avm/files/formatter/formats/html.rb
|
103
|
+
- lib/avm/files/formatter/formats/javascript.rb
|
104
|
+
- lib/avm/files/formatter/formats/json.rb
|
105
|
+
- lib/avm/files/formatter/formats/php.rb
|
106
|
+
- lib/avm/files/formatter/formats/python.rb
|
107
|
+
- lib/avm/files/formatter/formats/ruby.rb
|
108
|
+
- lib/avm/files/formatter/formats/xml.rb
|
109
|
+
- lib/avm/files/formatter/utf8_assert.rb
|
110
|
+
- lib/avm/files/rotate.rb
|
111
|
+
- lib/avm/files/version.rb
|
112
|
+
homepage:
|
113
|
+
licenses: []
|
114
|
+
metadata: {}
|
115
|
+
post_install_message:
|
116
|
+
rdoc_options: []
|
117
|
+
require_paths:
|
118
|
+
- lib
|
119
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
120
|
+
requirements:
|
121
|
+
- - ">="
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: '0'
|
124
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
125
|
+
requirements:
|
126
|
+
- - ">="
|
127
|
+
- !ruby/object:Gem::Version
|
128
|
+
version: '0'
|
129
|
+
requirements: []
|
130
|
+
rubygems_version: 3.1.6
|
131
|
+
signing_key:
|
132
|
+
specification_version: 4
|
133
|
+
summary: Put here de description.
|
134
|
+
test_files: []
|