meta-build 0.1.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 39d82dbf36468d0637db3a2e447dff8b1c748373
4
+ data.tar.gz: e78d79d92b8b36fe55a19ca930b4f0d57ce69ac4
5
+ SHA512:
6
+ metadata.gz: eadbb55ec968f9ebaa2feb5719b7c0b562a431c8d2c1a46d0bcf90bb48a69bdc1cc1e30403ffd28d5eaa9a1805c91196c8ae02bc277f1cc6eb7b3898c0e5dd9e
7
+ data.tar.gz: 4b56a8ac55129df308cf7911b1f102aed937268fb87196f4a7117cbb918f114629fb7f775ab5aca3149e258dd6a1145c15d64357421138731ccd384672437c35
@@ -0,0 +1,58 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+ require 'meta-build'
5
+ require 'meta_build/command_line'
6
+ require 'yaml'
7
+
8
+ options = {}
9
+ release = {
10
+ 'gem' => MetaBuild::GEM,
11
+ 'version' => MetaBuild::VERSION,
12
+ 'date' => MetaBuild::DATE
13
+ }
14
+
15
+ OptionParser.new do |opts|
16
+ opts.banner = "#{release['gem']}: Extract metadata from build artifacts"
17
+ opts.define_head "Usage: #{release['gem']} [options]"
18
+ opts.separator ""
19
+ opts.separator "Examples:"
20
+ opts.separator " #{release['gem']} -f file/path/to/artifact -o target/output/directory"
21
+ opts.separator " #{release['gem']} -f file/path/to/artifact -o target/output/directory"
22
+ opts.separator " #{release['gem']} -f file/path/to/artifact -o target/output/directory -r"
23
+ opts.separator " #{release['gem']} -f file/path/to/artifact -o target/output/directory -rc"
24
+ opts.separator ""
25
+ opts.separator "Options:"
26
+
27
+ opts.on('-f', '--file [artifact file path]', 'Example: --file path/to/target/artifact.ear') do |file|
28
+ options[:compressed_file] = file
29
+ end
30
+
31
+ opts.on('-o', '--output-dir [directory path]', 'Example: --output-dir tmp/target') do |directory|
32
+ options[:output_dir] = directory
33
+ end
34
+
35
+ opts.on('-n', '--name [target file simple name]', 'Example: --name pretty_file_name') do |name|
36
+ options[:name] = name
37
+ end
38
+
39
+ opts.on('-r', '--replace-file') do
40
+ options[:replace_files] = true
41
+ end
42
+
43
+ opts.on('-c', '--create-dir') do
44
+ options[:create_dir] = true
45
+ end
46
+
47
+ opts.on('-v', '--version') do
48
+ puts "#{release['gem']} #{release['version']} (#{release['date']})"
49
+ exit 0
50
+ end
51
+ end.parse!
52
+
53
+ begin
54
+ MetaBuild::CommandLine.execute(options)
55
+ rescue Exception => ex
56
+ puts "[ERROR] #{ex}" unless ex.instance_of? SystemExit
57
+ exit -1
58
+ end
@@ -0,0 +1 @@
1
+ require 'meta_build/app'
@@ -0,0 +1,78 @@
1
+ module MetaBuild
2
+
3
+ class App
4
+
5
+ require 'fileutils'
6
+ require 'zip'
7
+ require 'json'
8
+ require 'tmpdir'
9
+
10
+ require 'meta_build/version'
11
+ require 'meta_build/exceptions'
12
+ require 'meta_build/helper/app_helper'
13
+ require 'meta_build/helper/properties_helper'
14
+ require 'meta_build/helper/zip_helper'
15
+ require 'meta_build/extractor/base_extractor'
16
+ require 'meta_build/extractor/ear_extractor'
17
+ require 'meta_build/extractor/war_extractor'
18
+ require 'meta_build/extractor/jar_extractor'
19
+ require 'meta_build/extractor/extractor_factory'
20
+ require 'meta_build/parser/base_parser'
21
+ require 'meta_build/parser/ear_parser'
22
+ require 'meta_build/parser/war_parser'
23
+ require 'meta_build/parser/jar_parser'
24
+ require 'meta_build/parser/parser_factory'
25
+ require 'meta_build/builder/meta_builder'
26
+
27
+ attr_reader :compressed_file, :output_dir, :name, :replace_files, :create_dir
28
+
29
+ def initialize(options = {})
30
+ message = MetaBuild::Helper::AppHelper.validate_options options
31
+ if message
32
+ raise "[ERROR] #{message}"
33
+ end
34
+
35
+ @compressed_file = options[:compressed_file]
36
+ @output_dir = options[:output_dir]
37
+ @name = MetaBuild::Helper::AppHelper.set_target_name(options[:name], options[:compressed_file])
38
+ @replace_files = options[:replace_files] ||= false
39
+ @create_dir = options[:create_dir] ||= false
40
+ end
41
+
42
+ def extract_metadata
43
+ _configure_prerequisites
44
+
45
+ hash = MetaBuild::Builder::MetaBuilder.build file: @compressed_file
46
+
47
+ FileUtils.makedirs @output_dir unless File.exist? @output_dir
48
+ target_file = File.join(@output_dir, @name)
49
+
50
+ if File.exist?(target_file) && (!@replace_files)
51
+ puts "[ERROR] The file #{target_file} already exist. You might want to call this feature with the parameter [-r, --replace-file]"
52
+ exit 1
53
+ end
54
+
55
+ File.open(target_file, 'w') { |io| io.write hash.to_json }
56
+
57
+ FileUtils.remove_dir MetaBuild::Helper::AppHelper.work_dir, true
58
+ end
59
+
60
+ private
61
+ def _configure_prerequisites
62
+ _notify_create_dir
63
+ _notify_truncate_dir
64
+ end
65
+
66
+ def _notify_create_dir
67
+ return unless @create_dir
68
+ FileUtils.remove_dir @output_dir, true
69
+ end
70
+
71
+ def _notify_truncate_dir
72
+ return unless @replace_files
73
+ FileUtils.remove Dir.glob("#{@output_dir}/**/*")
74
+ end
75
+
76
+ end
77
+
78
+ end
@@ -0,0 +1,36 @@
1
+ module MetaBuild
2
+ module Builder
3
+
4
+ class MetaBuilder
5
+ class << self
6
+
7
+ def build(options = {})
8
+ metadata = nil
9
+
10
+ extractor = MetaBuild::Extractor::ExtractorFactory.build options
11
+ extractor.extract
12
+
13
+ options[:source_path] = extractor.tmp_dir
14
+ parser = MetaBuild::Parser::ParserFactory.build options
15
+ hash = parser.parse
16
+
17
+ metadata = hash
18
+ artifacts = Dir.glob("#{extractor.tmp_dir}/**/*.war").concat Dir.glob("#{extractor.tmp_dir}/**/*.jar")
19
+ metadata['dependencies'] = { 'wars' => [], 'jars' => [] } if artifacts.size > 0
20
+
21
+ artifacts.each do |artifact|
22
+ if artifact.end_with? 'war'
23
+ metadata['dependencies']['wars'] << self.build(file: artifact)
24
+ else
25
+ metadata['dependencies']['jars'] << self.build(file: artifact)
26
+ end
27
+ end
28
+
29
+ metadata
30
+ end
31
+
32
+ end
33
+ end
34
+
35
+ end
36
+ end
@@ -0,0 +1,27 @@
1
+ module MetaBuild
2
+ class CommandLine
3
+
4
+ def self.execute(options = {})
5
+ new(options).run
6
+ end
7
+
8
+ def initialize(options = {})
9
+ message = MetaBuild::Helper::AppHelper.validate_options options
10
+ if message
11
+ puts "[ERROR] #{message}"
12
+ exit 1
13
+ end
14
+
15
+ @options = options
16
+
17
+ @options[:name] = MetaBuild::Helper::AppHelper.set_target_name(@options[:name], @options[:compressed_file])
18
+ @options[:replace_files] ||= false
19
+ @options[:create_dir] ||= false
20
+ end
21
+
22
+ def run
23
+ MetaBuild::App.new(@options).extract_metadata
24
+ end
25
+
26
+ end
27
+ end
@@ -0,0 +1,5 @@
1
+ module MetaBuild
2
+ module Exceptions
3
+ class MetaBuildException < StandardError; end
4
+ end
5
+ end
@@ -0,0 +1,31 @@
1
+ module MetaBuild
2
+ module Extractor
3
+
4
+ class BaseExtractor
5
+
6
+ attr_accessor :file, :parent
7
+ attr_reader :artifact
8
+
9
+ def initialize(options = {})
10
+ @parent = options[:parent]
11
+ @file = File.absolute_path options[:file]
12
+
13
+ @artifact = File.basename @file.sub /\.(ear|war|jar)\z/, ''
14
+ end
15
+
16
+ def extract
17
+ raise MetaBuild::Exceptions::MetaBuildException.new "#{self.class}.extract must be overridden."
18
+ end
19
+
20
+ def tmp_dir
21
+ @temp ||= File.join Dir.tmpdir, 'meta-build', 'artifacts', @parent.to_s, @artifact
22
+ end
23
+
24
+ def create_tmp_dir
25
+ FileUtils.mkdir_p tmp_dir unless File.exist? tmp_dir
26
+ end
27
+
28
+ end
29
+
30
+ end
31
+ end
@@ -0,0 +1,14 @@
1
+ module MetaBuild
2
+ module Extractor
3
+
4
+ class EarExtractor < BaseExtractor
5
+
6
+ def extract
7
+ self.create_tmp_dir
8
+ MetaBuild::Helper::ZipHelper.extract @file, '', self.tmp_dir
9
+ end
10
+
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,21 @@
1
+ module MetaBuild
2
+ module Extractor
3
+
4
+ class ExtractorFactory
5
+ class << self
6
+
7
+ def build(options = {})
8
+ ext = File.extname(options[:file]).sub /\A\./, ''
9
+ case ext
10
+ when 'ear' then EarExtractor.new options
11
+ when 'war' then WarExtractor.new options
12
+ when 'jar' then JarExtractor.new options
13
+ else raise MetaBuild::Exceptions::MetaBuildException.new "Could not find suitable extractor to '#{ext.upcase}' type."
14
+ end
15
+ end
16
+
17
+ end
18
+ end
19
+
20
+ end
21
+ end
@@ -0,0 +1,14 @@
1
+ module MetaBuild
2
+ module Extractor
3
+
4
+ class JarExtractor < BaseExtractor
5
+
6
+ def extract
7
+ self.create_tmp_dir
8
+ MetaBuild::Helper::ZipHelper.extract @file, 'META-INF', self.tmp_dir
9
+ end
10
+
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,18 @@
1
+ module MetaBuild
2
+ module Extractor
3
+
4
+ class WarExtractor < BaseExtractor
5
+
6
+ EXPECTED_PATHS = %w(META-INF WEB-INF/lib public)
7
+
8
+ def extract
9
+ self.create_tmp_dir
10
+ EXPECTED_PATHS.each do |path|
11
+ MetaBuild::Helper::ZipHelper.extract @file, path, self.tmp_dir
12
+ end
13
+ end
14
+
15
+ end
16
+
17
+ end
18
+ end
@@ -0,0 +1,42 @@
1
+ module MetaBuild
2
+ module Helper
3
+
4
+ class AppHelper
5
+ class << self
6
+
7
+ def validate_options(options)
8
+ if options[:compressed_file].to_s.empty?
9
+ return "Parameter [-f, --file] is required."
10
+ elsif !File.exist?(options[:compressed_file])
11
+ return "File denoted by the path '#{options[:compressed_file]}' does not exist."
12
+ end
13
+
14
+ if options[:output_dir].to_s.empty?
15
+ return "Parameter [-o, --output-dir] is required."
16
+ elsif (!File.exist?(options[:output_dir])) && (options[:create_dir] != true)
17
+ return "Directory #{options[:output_dir]} does not exist. You might want to call this feature with the parameter [-c, --create-dir]"
18
+ end
19
+ end
20
+
21
+ def set_target_name(name = '', source_file)
22
+ name ||= ''
23
+
24
+ if name.empty?
25
+ name = source_file.split(File::SEPARATOR).last
26
+ name.sub! /\.\w+\z/, '.json'
27
+ elsif !name.end_with?('.json')
28
+ name << '.json'
29
+ end
30
+
31
+ name
32
+ end
33
+
34
+ def work_dir
35
+ File.join Dir.tmpdir, 'meta-build'
36
+ end
37
+
38
+ end
39
+ end
40
+
41
+ end
42
+ end
@@ -0,0 +1,26 @@
1
+ module MetaBuild
2
+ module Helper
3
+
4
+ class PropertiesHelper
5
+ class << self
6
+
7
+ def load(file)
8
+ return unless File.exist? file.to_s
9
+ props = Hash.new
10
+
11
+ File.readlines(file).each do |line|
12
+ line.strip!
13
+ next if (line.start_with? '#') || (line.empty?)
14
+
15
+ kv = line.split '='
16
+ props[kv.first.rstrip] = kv.last.lstrip
17
+ end
18
+
19
+ props
20
+ end
21
+
22
+ end
23
+ end
24
+
25
+ end
26
+ end
@@ -0,0 +1,30 @@
1
+ module MetaBuild
2
+ module Helper
3
+
4
+ class ZipHelper
5
+ class << self
6
+
7
+ def extract(file, path, target)
8
+ Zip::File.open(file) do |zip_file|
9
+ zip_file.each do |entry|
10
+ next unless entry.name.start_with? path
11
+
12
+ target_file = File.join(target, entry.name)
13
+ if entry.directory?
14
+ FileUtils.makedirs target_file
15
+ next
16
+ end
17
+
18
+ dir = File.dirname target_file
19
+ FileUtils.makedirs dir unless File.exist? dir
20
+
21
+ File.open(target_file, 'wb') { |io| io.write entry.get_input_stream.read }
22
+ end
23
+ end
24
+ end
25
+
26
+ end
27
+ end
28
+
29
+ end
30
+ end
@@ -0,0 +1,40 @@
1
+ module MetaBuild
2
+ module Parser
3
+
4
+ class BaseParser
5
+
6
+ attr_accessor :source_path
7
+
8
+ def initialize(options = {})
9
+ @source_path = options[:source_path]
10
+ end
11
+
12
+ def parse
13
+ raise MetaBuild::Exceptions::MetaBuildException.new "#{self.class}.parse must be overridden."
14
+ end
15
+
16
+ protected
17
+ def _parse
18
+ path = "#{@source_path}/META-INF/maven/**/pom.properties"
19
+ hash = MetaBuild::Helper::PropertiesHelper.load Dir.glob(path).first
20
+
21
+ if hash.nil?
22
+ captures = /([-\w\d]+)-([\w\d\.]+)\z/.match(@source_path).captures
23
+ hash = {
24
+ 'artifactId' => captures.first,
25
+ 'version' => captures.last
26
+ }
27
+ end
28
+
29
+ data_json = "#{@source_path}/META-INF/data.json"
30
+ if File.exist? data_json
31
+ meta_inf = JSON.parse File.read(data_json)
32
+ hash.merge! meta_inf
33
+ end
34
+
35
+ hash
36
+ end
37
+ end
38
+
39
+ end
40
+ end
@@ -0,0 +1,16 @@
1
+ module MetaBuild
2
+ module Parser
3
+
4
+ class EarParser < BaseParser
5
+
6
+ def parse
7
+ hash = self._parse
8
+ hash['type'] = 'ear'
9
+
10
+ hash
11
+ end
12
+
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ module MetaBuild
2
+ module Parser
3
+
4
+ class JarParser < BaseParser
5
+
6
+ def parse
7
+ hash = self._parse
8
+ hash['type'] = 'jar'
9
+
10
+ hash
11
+ end
12
+
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,21 @@
1
+ module MetaBuild
2
+ module Parser
3
+
4
+ class ParserFactory
5
+ class << self
6
+
7
+ def build(options = {})
8
+ ext = File.extname(options[:file]).sub /\A\./, ''
9
+ case ext
10
+ when 'ear' then EarParser.new options
11
+ when 'war' then WarParser.new options
12
+ when 'jar' then JarParser.new options
13
+ else raise MetaBuild::Exceptions::MetaBuildException.new "Could not find suitable parser to '#{ext.upcase}' type."
14
+ end
15
+ end
16
+
17
+ end
18
+ end
19
+
20
+ end
21
+ end
@@ -0,0 +1,48 @@
1
+ module MetaBuild
2
+ module Parser
3
+
4
+ class WarParser < BaseParser
5
+
6
+ MODULES = {
7
+ 'siefdau-web' => 'sdau',
8
+ 'rdoc-web' => 'rdoc',
9
+ 'cobra-web' => 'cobra',
10
+ 'tdecis-web' => 'tdcs',
11
+ 'precadin-web' => 'pcad',
12
+ 'mpct-web' => 'overlay',
13
+ 'sic-web' => 'sic'
14
+ }
15
+
16
+ def parse
17
+ path = "#{@source_path}/META-INF/maven/**/pom.properties"
18
+ hash = Hash.new
19
+
20
+ Dir.glob(path).each do |pom|
21
+ pieces = pom.split(File::SEPARATOR)
22
+ _module = pieces[pieces.size - 2]
23
+ hash[MODULES[_module]] = MetaBuild::Helper::PropertiesHelper.load(pom)
24
+ end
25
+
26
+ path = "#{@source_path}/public/**/data.json"
27
+
28
+ Dir.glob(path).each do |data_json|
29
+ meta_inf = JSON.parse File.read(data_json)
30
+
31
+ pieces = data_json.split(File::SEPARATOR)
32
+ _module = pieces[pieces.size - 2]
33
+ _module = MODULES['mpct-web'] if _module == 'public'
34
+
35
+ if hash[_module]
36
+ hash[_module].merge! meta_inf
37
+ else
38
+ hash[_module] = meta_inf
39
+ end
40
+ end
41
+
42
+ hash
43
+ end
44
+
45
+ end
46
+
47
+ end
48
+ end
@@ -0,0 +1,5 @@
1
+ module MetaBuild
2
+ GEM ||= 'meta-build'
3
+ VERSION ||= '0.1.0'
4
+ DATE ||= '2016-08-25'
5
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: meta-build
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Aurealino
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-08-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rubyzip
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 1.2.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 1.2.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '='
32
+ - !ruby/object:Gem::Version
33
+ version: 3.5.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '='
39
+ - !ruby/object:Gem::Version
40
+ version: 3.5.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: rest-client
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '='
46
+ - !ruby/object:Gem::Version
47
+ version: 2.0.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '='
53
+ - !ruby/object:Gem::Version
54
+ version: 2.0.0
55
+ description: Biblioteca Ruby para extração de meta-informações dos artefatos de construção
56
+ do SIC.
57
+ email: aureliano.franca@serpro.gov.br
58
+ executables:
59
+ - meta-build
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - bin/meta-build
64
+ - lib/meta-build.rb
65
+ - lib/meta_build/app.rb
66
+ - lib/meta_build/builder/meta_builder.rb
67
+ - lib/meta_build/command_line.rb
68
+ - lib/meta_build/exceptions.rb
69
+ - lib/meta_build/extractor/base_extractor.rb
70
+ - lib/meta_build/extractor/ear_extractor.rb
71
+ - lib/meta_build/extractor/extractor_factory.rb
72
+ - lib/meta_build/extractor/jar_extractor.rb
73
+ - lib/meta_build/extractor/war_extractor.rb
74
+ - lib/meta_build/helper/app_helper.rb
75
+ - lib/meta_build/helper/properties_helper.rb
76
+ - lib/meta_build/helper/zip_helper.rb
77
+ - lib/meta_build/parser/base_parser.rb
78
+ - lib/meta_build/parser/ear_parser.rb
79
+ - lib/meta_build/parser/jar_parser.rb
80
+ - lib/meta_build/parser/parser_factory.rb
81
+ - lib/meta_build/parser/war_parser.rb
82
+ - lib/meta_build/version.rb
83
+ homepage: https://git.serpro/sic/meta-build
84
+ licenses:
85
+ - Nonstandard
86
+ metadata: {}
87
+ post_install_message:
88
+ rdoc_options:
89
+ - "--charset=UTF-8"
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: 1.9.3
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubyforge_project:
104
+ rubygems_version: 2.5.1
105
+ signing_key:
106
+ specification_version: 4
107
+ summary: meta-build-0.1.0 Biblioteca para extração de meta-informações do SIC.
108
+ test_files: []