code-formatter 0.0.12 → 0.0.13

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
  SHA256:
3
- metadata.gz: dfd1e2ccc28235296e5b318a547d80f108be3fb4e0f31efcc167680030a129c4
4
- data.tar.gz: 3c5da8161f062fe201805689190512634e1f0c6dfda09c0de5f1408301eaa217
3
+ metadata.gz: 865105745c3f2d3fb50fc610d8f6971159fc4f2fa45bb9c8fac3e3e2c5a3b1e3
4
+ data.tar.gz: 5aad10ead21110a08016fb61154ecc720ecbcc5c8ad42647a1bc5e1676c0833e
5
5
  SHA512:
6
- metadata.gz: 92196d4d5c50aac1218ed7b8eefe1ea77b389a51b7d4e733666894388f941a7081b8f11f50d9d52fa4b6f5aceae809c75ba08db0137ec29f74b1eb224b13dd3d
7
- data.tar.gz: 50c7c115f88f78ba9c874f78c59b2c81ab2454dd1390decf440b5b5a3ad607e8e8cc1d4431aadff5767fd5e7d9cedcf864e66ab02045c0a6d43121c883b548df
6
+ metadata.gz: 180aad68a1b54483167924217eda171f1090ddd5c28eccd9043d2a18b73d19aaa6ef1c9913646719f39519ef82b3ac940457fa086d6f67f12909804e1c929a02
7
+ data.tar.gz: 6d408f0b1cba8c2885b368fdc2334375de5e1cabf7958227288047c93895b1c3aaea6a7e4d597e4736ae3fe224ae57d17725357ffc9b6fe4fb438e5b6bc883f7
@@ -1,6 +1,7 @@
1
1
  require_relative 'initializer'
2
2
  require_relative 'configuration'
3
3
  require_relative 'formatters/swift_format_formatter'
4
+ require_relative 'formatters/swift_lint_formatter'
4
5
  require 'thor'
5
6
 
6
7
  module CodeFormatter
@@ -15,7 +16,7 @@ module CodeFormatter
15
16
  def format
16
17
  config_file = options[:config]
17
18
  config = Configuration.load_from(config_file)
18
- formatters = [SwiftFormatFormatter.new(config)]
19
+ formatters = [SwiftFormatFormatter.new(config), SwiftLintFormatter.new(config)]
19
20
  formatters.each(&:format)
20
21
  end
21
22
  end
@@ -2,15 +2,11 @@ module CodeFormatter
2
2
  class Configuration
3
3
  attr_accessor :included_files
4
4
  attr_accessor :excluded_files
5
- attr_accessor :included_dirs
6
- attr_accessor :excluded_dirs
7
5
  attr_accessor :source_file
8
6
 
9
7
  def initialize
10
8
  self.included_files = []
11
9
  self.excluded_files = []
12
- self.included_dirs = []
13
- self.excluded_dirs = []
14
10
 
15
11
  yield self if block_given?
16
12
  end
@@ -0,0 +1,7 @@
1
+ module CodeFormatter
2
+ class Environment
3
+ def self.exist?(command)
4
+ !`which #{command}`.empty?
5
+ end
6
+ end
7
+ end
@@ -1,10 +1,15 @@
1
+ require_relative '../environment'
2
+
1
3
  module CodeFormatter
2
4
  class Formatter
3
5
  attr_reader :config
6
+ attr_reader :entry_point
4
7
 
5
- def initialize(config)
8
+ def initialize(config, entry_point = Dir.pwd)
6
9
  raise TypeError unless config.is_a? Configuration
10
+ raise TypeError unless Dir.exist?(entry_point)
7
11
  @config = config
12
+ @entry_point = entry_point
8
13
  end
9
14
 
10
15
  def format
@@ -1,24 +1,24 @@
1
- require 'yaml'
2
1
  require_relative 'formatter'
2
+ require 'yaml'
3
3
 
4
4
  module CodeFormatter
5
5
  class SwiftFormatFormatter < Formatter
6
6
  def format
7
- unless exist? 'git'
7
+ unless Environment.exist? 'git'
8
8
  warn 'warning: failed to find git command'
9
9
  return
10
10
  end
11
11
 
12
- unless exist? 'swiftformat'
12
+ unless Environment.exist? 'swiftformat'
13
13
  warn "warning: [swiftformat] not found (to install it run 'brew install swiftformat')"
14
14
  return
15
15
  end
16
16
 
17
17
  files = files_to_format
18
18
  if files.empty?
19
- puts 'everything is up-to-date'
19
+ puts '[swiftformat]: everything is up-to-date'
20
20
  else
21
- puts "going to format files:\n#{files.join('\n')}"
21
+ puts "[swiftformat]: going to format files:\n#{files.join('\n')}"
22
22
  format_files(files)
23
23
  end
24
24
  end
@@ -30,11 +30,24 @@ module CodeFormatter
30
30
  modified_and_untracked = `git ls-files --others --exclude-standard -m -- #{filter}`
31
31
  staged = `git diff --cached --name-only -- #{filter}`
32
32
  all_files = modified_and_untracked + staged
33
- all_files.lines.uniq.map { |f| File.expand_path f.strip }.select { |f| File.exist? f }
33
+ all_files.lines.uniq.map { |f| File.expand_path f.strip }.select { |f| format_file? f }
34
+ end
35
+
36
+ def format_file?(file)
37
+ puts file
38
+ return false unless File.file?(file) && File.exist?(file)
39
+ return false if excluded_file?(file)
40
+ included_file?(file)
41
+ end
42
+
43
+ def included_file?(file)
44
+ included_files = config.included_files.map { |f| File.expand_path(f, entry_point) }
45
+ included_files.any? { |f| file.start_with?(f) }
34
46
  end
35
47
 
36
- def exist?(command)
37
- !`which #{command}`.empty?
48
+ def excluded_file?(file)
49
+ excluded_files = config.excluded_files.map { |f| File.expand_path(f, entry_point) }
50
+ excluded_files.any? { |f| file.start_with?(f) }
38
51
  end
39
52
 
40
53
  def format_files(files)
@@ -0,0 +1,40 @@
1
+ require_relative 'formatter'
2
+ require 'yaml'
3
+ require 'tempfile'
4
+
5
+ module CodeFormatter
6
+ class SwiftLintFormatter < Formatter
7
+ def format
8
+ unless Environment.exist? 'swiftlint'
9
+ warn "warning: [swiftlint] not found (to install it run 'brew install swiftlint')"
10
+ return
11
+ end
12
+
13
+ create_config do |file|
14
+ `swiftlint autocorrect --config #{file.path}`
15
+ `swiftlint --config #{file.path}`
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ def create_config
22
+ file = Tempfile.new(%w[swiftlint .yml])
23
+ begin
24
+ file.write(swiftlint_config)
25
+ file.close
26
+ yield file if block_given?
27
+ ensure
28
+ file.unlink
29
+ end
30
+ end
31
+
32
+ def swiftlint_config
33
+ rules_file = File.expand_path('../../resources/swiftlint.yml', __FILE__)
34
+ yaml_hash = YAML.load_file(rules_file)
35
+ yaml_hash['included'] = config.included_files
36
+ yaml_hash['excluded'] = config.excluded_files
37
+ yaml_hash.to_yaml
38
+ end
39
+ end
40
+ end
@@ -1,6 +1,4 @@
1
1
  CodeFormatter::Configuration.new do |c|
2
2
  c.included_files = ['path/to/included/file.ext']
3
3
  c.excluded_files = ['path/to/excluded/file.ext']
4
- c.included_dirs = ['path/to/included/dir']
5
- c.excluded_dirs = ['path/to/excluded/dir']
6
4
  end
@@ -0,0 +1,21 @@
1
+ disabled_rules:
2
+ - line_length
3
+ - type_body_length
4
+ - identifier_name
5
+ - type_name
6
+ - weak_delegate
7
+ - redundant_string_enum_value
8
+ - notification_center_detachment
9
+ - unused_closure_parameter
10
+ - function_parameter_count
11
+ - cyclomatic_complexity
12
+
13
+ force_cast: warning
14
+
15
+ file_length:
16
+ warning: 500
17
+ error: 1000
18
+
19
+ function_body_length:
20
+ warning: 70
21
+ error: 100
@@ -1,3 +1,3 @@
1
1
  module CodeFormatter
2
- VERSION = '0.0.12'.freeze
2
+ VERSION = '0.0.13'.freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: code-formatter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.12
4
+ version: 0.0.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - sroik
@@ -106,11 +106,14 @@ files:
106
106
  - lib/code_formatter.rb
107
107
  - lib/code_formatter/cli.rb
108
108
  - lib/code_formatter/configuration.rb
109
+ - lib/code_formatter/environment.rb
109
110
  - lib/code_formatter/formatters/formatter.rb
110
111
  - lib/code_formatter/formatters/swift_format_formatter.rb
112
+ - lib/code_formatter/formatters/swift_lint_formatter.rb
111
113
  - lib/code_formatter/initializer.rb
112
114
  - lib/code_formatter/resources/sample_config.config
113
115
  - lib/code_formatter/resources/swiftformat_rules.yml
116
+ - lib/code_formatter/resources/swiftlint.yml
114
117
  - lib/code_formatter/version.rb
115
118
  homepage: https://github.com/app-craft/code-formatter
116
119
  licenses: