platina_world 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f31d00b3ea593d1fa7c061549b79b11d97ee355b
4
- data.tar.gz: bbac5d46e8c508e5f9edd4e70478e0c8f61e6e89
3
+ metadata.gz: a58fa49f81c1f28bf2a25221dfad33a4fbf7a3e0
4
+ data.tar.gz: cdbe1c6cf7ce7b30a1772beb6698cb517b3746ff
5
5
  SHA512:
6
- metadata.gz: eacb10bc3083672ecae9303924db160f0183de885d8efc2693cae61ba4c9fc45e551997dfe16b64ac034401a2e6c24eaf22ab1ecc059d0d5083c4f839bdc03b9
7
- data.tar.gz: 8104a3ec927bbbce3586436b20fef34482ab4071b8941ac38f96ebd6427d669d4eb96fa6f23311ac173078d7438810cec5599f880b60dd197d983c5983db9867
6
+ metadata.gz: f70c876bbe5bc5209625ba3709ae2eb298a7abc0cd137c94da322060a67b168aa48fdc9aa93d7422b42637bdd949a921e9b3e3046d3d1d8c33333e712217cc1e
7
+ data.tar.gz: 200c924738b77a4cddb8eac773ae0901d2d15b532c447c5cf8ae1a7bcb0b4e719ac578b232a4a4d3d7398b9215588ff0ef6f59a7b5340027620f69c244136b9a
data/lib/platina_world.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require "platina_world/version"
2
2
  require "platina_world/command"
3
+ require "platina_world/logger"
3
4
 
4
5
  module PlatinaWorld
5
6
  end
@@ -1,5 +1,7 @@
1
1
  require "optparse"
2
- require "platina_world/runner"
2
+ require "platina_world/runners/dry"
3
+ require "platina_world/runners/production"
4
+ require "platina_world/file_loader"
3
5
 
4
6
  module PlatinaWorld
5
7
  class Command
@@ -14,7 +16,27 @@ module PlatinaWorld
14
16
  private
15
17
 
16
18
  def runner
17
- @runner ||= PlatinaWorld::Runner.new(options)
19
+ runner_class.new(loaded_file)
20
+ end
21
+
22
+ def runner_class
23
+ if dry_run?
24
+ PlatinaWorld::Runners::Dry
25
+ else
26
+ PlatinaWorld::Runners::Production
27
+ end
28
+ end
29
+
30
+ def loaded_file
31
+ @loaded_file ||= PlatinaWorld::FileLoader.new(file_path).load
32
+ end
33
+
34
+ def file_path
35
+ options["path"]
36
+ end
37
+
38
+ def dry_run?
39
+ options["dry-run"]
18
40
  end
19
41
 
20
42
  def options
@@ -24,7 +46,8 @@ module PlatinaWorld
24
46
  def option_parser
25
47
  @optin_parser ||= OptionParser.new do |opt|
26
48
  opt.version = PlatinaWorld::VERSION
27
- opt.on("-p", "--path [file]", "Configuration file path") { |file_path| file_path }
49
+ opt.on("-p", "--path [file]", "Configuration file path")
50
+ opt.on("-n", "--dry-run", "run in dry-run mode")
28
51
  end
29
52
  end
30
53
  end
@@ -0,0 +1,6 @@
1
+ module PlatinaWorld
2
+ module Errors
3
+ class Base < StandardError
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,15 @@
1
+ require "platina_world/errors/base"
2
+
3
+ module PlatinaWorld
4
+ module Errors
5
+ class FilePathError < Base
6
+ def initialize(file_path)
7
+ @file_path = file_path
8
+ end
9
+
10
+ def to_s
11
+ "File does not exist: `#{@file_path}`"
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,21 @@
1
+ require "platina_world/errors/base"
2
+
3
+ module PlatinaWorld
4
+ module Errors
5
+ class InvalidExtensionError < Base
6
+ def initialize(acceptable_extensions)
7
+ @acceptable_extensions = acceptable_extensions
8
+ end
9
+
10
+ def to_s
11
+ "Extension is invalid, please pass type of #{types}"
12
+ end
13
+
14
+ private
15
+
16
+ def types
17
+ @acceptable_extensions.join(" or ")
18
+ end
19
+ end
20
+ end
21
+ end
@@ -1,20 +1,22 @@
1
1
  require "yaml"
2
+ require "platina_world/errors/file_path_error"
3
+ require "platina_world/errors/invalid_extension_error"
2
4
 
3
5
  module PlatinaWorld
4
6
  class FileLoader
5
7
  DEFAULT_FILE_PATH = "./platina_world.yml".freeze
6
- ACCEPT_EXT_NAMES = %w(.yml .yaml).freeze
8
+ ACCEPTABLE_EXTENSIONS = %w(.yml .yaml).freeze
7
9
 
8
- def initialize(file_path = DEFAULT_FILE_PATH)
10
+ def initialize(file_path)
9
11
  @file_path = file_path
10
12
  end
11
13
 
12
14
  def load
13
15
  case
14
16
  when !exist_file?
15
- raise "#{file_path} is not found."
16
- when !valid_ext_name?
17
- raise "Ext name is invalid. plase pass .yml or .yaml file"
17
+ PlatinaWorld::Logger.error(file_path_error) and abort
18
+ when !valid_extension_type?
19
+ PlatinaWorld::Logger.error(invalid_extion_error) and abort
18
20
  else
19
21
  file_load
20
22
  end
@@ -22,7 +24,17 @@ module PlatinaWorld
22
24
 
23
25
  private
24
26
 
25
- attr_reader :file_path
27
+ def file_path_error
28
+ PlatinaWorld::Errors::FilePathError.new(file_path)
29
+ end
30
+
31
+ def invalid_extion_error
32
+ PlatinaWorld::Errors::InvalidExtensionError.new(ACCEPTABLE_EXTENSIONS)
33
+ end
34
+
35
+ def file_path
36
+ @file_path ||= DEFAULT_FILE_PATH
37
+ end
26
38
 
27
39
  def file_load
28
40
  YAML.load_file(file_path)
@@ -32,8 +44,8 @@ module PlatinaWorld
32
44
  File.exist?(file_path)
33
45
  end
34
46
 
35
- def valid_ext_name?
36
- ACCEPT_EXT_NAMES.include?(File.extname(file_path))
47
+ def valid_extension_type?
48
+ ACCEPTABLE_EXTENSIONS.include?(File.extname(file_path))
37
49
  end
38
50
  end
39
51
  end
@@ -0,0 +1,41 @@
1
+ module PlatinaWorld
2
+ module Generators
3
+ class Base
4
+ def initialize(paths)
5
+ @paths = paths
6
+ end
7
+
8
+ def call
9
+ @paths.each do |path|
10
+ case
11
+ when path.directory?
12
+ generate_directory(path)
13
+ when path.has_directory?
14
+ generate_file_with_dir(path)
15
+ else
16
+ generate_file(path)
17
+ end
18
+
19
+ PlatinaWorld::FileStatus.info(
20
+ action: "create",
21
+ path: path.file_path
22
+ )
23
+ end
24
+ end
25
+
26
+ private
27
+
28
+ def generate_directory(path)
29
+ raise NotImplementedError
30
+ end
31
+
32
+ def generate_file(path)
33
+ raise NotImplementedError
34
+ end
35
+
36
+ def generate_file_with_dir(path)
37
+ raise NotImplementedError
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,23 @@
1
+ require "platina_world/generators/base"
2
+ require "fileutils"
3
+
4
+ module PlatinaWorld
5
+ module Generators
6
+ class File < Base
7
+ private
8
+
9
+ def generate_directory(path)
10
+ FileUtils.mkdir_p(path.directory_name)
11
+ end
12
+
13
+ def generate_file(path)
14
+ FileUtils.touch(path.file_name)
15
+ end
16
+
17
+ def generate_file_with_dir(path)
18
+ FileUtils.mkdir_p(path.directory_name)
19
+ FileUtils.touch("#{path.directory_name}/#{path.file_name}")
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,21 @@
1
+ require "platina_world/generators/base"
2
+
3
+ module PlatinaWorld
4
+ module Generators
5
+ class Mock < Base
6
+ private
7
+
8
+ def generate_directory(path)
9
+ # noop
10
+ end
11
+
12
+ def generate_file(path)
13
+ # noop
14
+ end
15
+
16
+ def generate_file_with_dir(path)
17
+ # noop
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,7 @@
1
+ require "platina_world/loggers/file_status"
2
+ require "platina_world/loggers/logger"
3
+
4
+ module PlatinaWorld
5
+ FileStatus = PlatinaWorld::Loggers::FileStatus.new($stdout)
6
+ Logger = PlatinaWorld::Loggers::Logger.new($stdout)
7
+ end
@@ -0,0 +1,18 @@
1
+ require "logger"
2
+
3
+ module PlatinaWorld
4
+ module Loggers
5
+ class Base < ::Logger
6
+ def initialize(*)
7
+ super
8
+ self.formatter = formatter_class.new
9
+ end
10
+
11
+ private
12
+
13
+ def formatter_class
14
+ raise NotImplementedError
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,28 @@
1
+ require "platina_world/loggers/base"
2
+
3
+ module PlatinaWorld
4
+ module Loggers
5
+ class FileStatus < Base
6
+ private
7
+
8
+ def formatter_class
9
+ PlatinaWorld::Loggers::FileFormatter
10
+ end
11
+ end
12
+
13
+ class FileFormatter
14
+ def call(severity, time, program_name, message)
15
+ file_format(severity) % message
16
+ end
17
+
18
+ def file_format(severity)
19
+ case severity
20
+ when "INFO"
21
+ " \e[32m%{action}\e[0m %{path}\n"
22
+ when "ERROR"
23
+ " \e[31m%{action}\e[0m %{path}\n"
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,24 @@
1
+ require "platina_world/loggers/base"
2
+
3
+ module PlatinaWorld
4
+ module Loggers
5
+ class Logger < Base
6
+ private
7
+
8
+ def formatter_class
9
+ PlatinaWorld::Loggers::Formatter
10
+ end
11
+ end
12
+
13
+ class Formatter
14
+ def call(severity, time, program_name, message)
15
+ case severity
16
+ when "INFO"
17
+ " \e[32m#{message}\n\e[0m"
18
+ when "ERROR"
19
+ " \e[31m#{message}\n\e[0m"
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -1,5 +1,7 @@
1
1
  module PlatinaWorld
2
2
  class Path
3
+ attr_reader :file_path
4
+
3
5
  def initialize(file_path)
4
6
  @file_path = file_path
5
7
  end
@@ -22,8 +24,6 @@ module PlatinaWorld
22
24
 
23
25
  private
24
26
 
25
- attr_reader :file_path
26
-
27
27
  # Return file name and dirctory name as Array
28
28
  # @params [String] file path (e.g "a/b/c")
29
29
  # @return [[String, String]] first is file path name and second is directory path name
@@ -2,12 +2,12 @@ require "platina_world/path"
2
2
 
3
3
  module PlatinaWorld
4
4
  class PathBuilder
5
- def initialize(loaded_data)
6
- @loaded_data = loaded_data
5
+ def initialize(loaded_file)
6
+ @loaded_file = loaded_file
7
7
  end
8
8
 
9
9
  def build
10
- generate_paths(@loaded_data).flat_map do |file_path|
10
+ generate_paths(@loaded_file).flat_map do |file_path|
11
11
  PlatinaWorld::Path.new(file_path)
12
12
  end
13
13
  end
@@ -0,0 +1,29 @@
1
+ require "platina_world/path_builder"
2
+
3
+ module PlatinaWorld
4
+ module Runners
5
+ class Base
6
+ def initialize(loaded_file)
7
+ @loaded_file = loaded_file
8
+ end
9
+
10
+ def run
11
+ file_generator.call
12
+ end
13
+
14
+ private
15
+
16
+ def generator_class
17
+ raise NotImplementedError
18
+ end
19
+
20
+ def file_generator
21
+ generator_class.new(paths)
22
+ end
23
+
24
+ def paths
25
+ @paths ||= PlatinaWorld::PathBuilder.new(@loaded_file).build
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,14 @@
1
+ require "platina_world/runners/base"
2
+ require "platina_world/generators/mock"
3
+
4
+ module PlatinaWorld
5
+ module Runners
6
+ class Dry < Base
7
+ private
8
+
9
+ def generator_class
10
+ PlatinaWorld::Generators::Mock
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ require "platina_world/runners/base"
2
+ require "platina_world/generators/file"
3
+
4
+ module PlatinaWorld
5
+ module Runners
6
+ class Production < Base
7
+ private
8
+
9
+ def generator_class
10
+ PlatinaWorld::Generators::File
11
+ end
12
+ end
13
+ end
14
+ end
@@ -1,3 +1,3 @@
1
1
  module PlatinaWorld
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -0,0 +1,20 @@
1
+ [
2
+ {
3
+ "lib": [
4
+ {
5
+ "platinaworld": []
6
+ },
7
+ "platinaworld.rb"
8
+ ]
9
+ },
10
+ {
11
+ "spec": [
12
+ {
13
+ "platinaworld": ["platinaworld_spec.rb"]
14
+ },
15
+ "spec_helper.rb"
16
+ ]
17
+ },
18
+ ".rspec",
19
+ ".gitignore"
20
+ ]
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: platina_world
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - ganmacs
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-08-29 00:00:00.000000000 Z
11
+ date: 2015-08-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -71,14 +71,26 @@ files:
71
71
  - bin/setup
72
72
  - lib/platina_world.rb
73
73
  - lib/platina_world/command.rb
74
- - lib/platina_world/file_generator.rb
74
+ - lib/platina_world/errors/base.rb
75
+ - lib/platina_world/errors/file_path_error.rb
76
+ - lib/platina_world/errors/invalid_extension_error.rb
75
77
  - lib/platina_world/file_loader.rb
78
+ - lib/platina_world/generators/base.rb
79
+ - lib/platina_world/generators/file.rb
80
+ - lib/platina_world/generators/mock.rb
81
+ - lib/platina_world/logger.rb
82
+ - lib/platina_world/loggers/base.rb
83
+ - lib/platina_world/loggers/file_status.rb
84
+ - lib/platina_world/loggers/logger.rb
76
85
  - lib/platina_world/path.rb
77
86
  - lib/platina_world/path_builder.rb
78
- - lib/platina_world/runner.rb
87
+ - lib/platina_world/runners/base.rb
88
+ - lib/platina_world/runners/dry.rb
89
+ - lib/platina_world/runners/production.rb
79
90
  - lib/platina_world/version.rb
80
91
  - platinaworld.gemspec
81
92
  - sample/ruby_world_sample.yml
93
+ - sample/simple_sample.json
82
94
  - sample/simple_sample.yml
83
95
  homepage: https://github.com/ganmacs/platina_world
84
96
  licenses: []
@@ -1,38 +0,0 @@
1
- require "platina_world/path_builder"
2
- require "fileutils"
3
-
4
- module PlatinaWorld
5
- class FileGenerator
6
- def initialize(paths)
7
- @paths = paths
8
- end
9
-
10
- def call
11
- @paths.each do |path|
12
- case
13
- when path.directory?
14
- generate_directory(path)
15
- when path.has_directory?
16
- generate_file_with_dir(path)
17
- else
18
- generate_file(path)
19
- end
20
- end
21
- end
22
-
23
- private
24
-
25
- def generate_directory(path)
26
- FileUtils.mkdir_p(path.directory_name)
27
- end
28
-
29
- def generate_file(path)
30
- FileUtils.touch(path.file_name)
31
- end
32
-
33
- def generate_file_with_dir(path)
34
- FileUtils.mkdir_p(path.directory_name)
35
- FileUtils.touch("#{path.directory_name}/#{path.file_name}")
36
- end
37
- end
38
- end
@@ -1,33 +0,0 @@
1
- require "platina_world/file_loader"
2
- require "platina_world/file_generator"
3
- require "platina_world/path_builder"
4
-
5
- module PlatinaWorld
6
- class Runner
7
- def initialize(options)
8
- @options = options
9
- end
10
-
11
- def run
12
- file_generator.call
13
- end
14
-
15
- private
16
-
17
- def file_generator
18
- PlatinaWorld::FileGenerator.new(paths)
19
- end
20
-
21
- def paths
22
- @paths ||= PlatinaWorld::PathBuilder.new(loaded_data).build
23
- end
24
-
25
- def loaded_data
26
- @loaded_data ||= PlatinaWorld::FileLoader.new(file_path).load
27
- end
28
-
29
- def file_path
30
- @options["file"]
31
- end
32
- end
33
- end