code-formatter 1.0.6 → 1.1.0

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: d4c7b0f3c7297e02c389288c3d100ff1c016b2918a0d279ca933ebef96173333
4
- data.tar.gz: 5749cdbdd67817ec0ecebd120821fb711d0a13f8535437b8594942743cf147ef
3
+ metadata.gz: 5657be24135ba626de8738ccf7348eb048eee38f937234491bcbdba18a5a24e9
4
+ data.tar.gz: 52a9238dec5aa5187273f473388f9d7595aee49066ae3bbf2d3743b8a3b4e636
5
5
  SHA512:
6
- metadata.gz: 1d6dcd3ca58b9c1911079893d3ea222a787261bd20fa409090af00d9892963ccd7a024f9a55bd182f3282280b498ffb12034cb9c070e4057416a71c9df30c2df
7
- data.tar.gz: 0aa865fe99d78f72de31a88aa4021d9c66a4c127ba796d5bb0b256c49c4ecccac55514c45d16c2149630352e878bb69299eebbac35a3ee7ad91e836330d33320
6
+ metadata.gz: 48bb4d1682ac1fb8f89ef27584347b65f658a9801ed8b6df6464384b22b085fba6483fcd45f44c3f6c4b70880930acc30e3c5d73ab4e9a05dbf85f5efc64a781
7
+ data.tar.gz: 8ef5f4598e212fb5cc0086440aff0d4ce9f51154317c7412835fd6e62c96d94194e2fc6ae8baf1dbe835de787ea8c01a25a85ade7f14f49f52b1f9896650e395
@@ -4,6 +4,7 @@ require_relative 'initializer'
4
4
  require_relative 'configuration'
5
5
  require_relative 'formatters/swift_format_formatter'
6
6
  require_relative 'formatters/swift_lint_formatter'
7
+ require_relative 'formatters/clang_format_formatter'
7
8
  require 'thor'
8
9
 
9
10
  module CodeFormatter
@@ -21,7 +22,11 @@ module CodeFormatter
21
22
  config = Configuration.load_from(options[:config])
22
23
  force_all = options[:force_all]
23
24
  path = options[:path]
24
- formatters = [SwiftFormatFormatter.new(config, path), SwiftLintFormatter.new(config, path)]
25
+ formatters = [
26
+ SwiftFormatFormatter.new(config, path),
27
+ SwiftLintFormatter.new(config, path),
28
+ ClangFormatFormatter.new(config, path)
29
+ ]
25
30
  formatters.each { |f| f&.format(force_all) }
26
31
  end
27
32
  end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'formatter'
4
+ require 'yaml'
5
+
6
+ module CodeFormatter
7
+ class ClangFormatFormatter < Formatter
8
+ def format_files(files)
9
+ `clang-format -i -style="#{style_dump}" #{files.join(' ')}`
10
+ end
11
+
12
+ def shell_command
13
+ 'clang-format'
14
+ end
15
+
16
+ def files_filter
17
+ "'*.h' '*.m' '*.mm' '*.c' '*.cpp'"
18
+ end
19
+
20
+ private
21
+
22
+ def style_dump
23
+ style_yaml.to_s.gsub('=>', ' : ')
24
+ end
25
+
26
+ def style_yaml
27
+ file = File.expand_path('../../resources/clang_format_style.yml', __FILE__)
28
+ YAML.load_file(file)
29
+ end
30
+ end
31
+ end
@@ -14,8 +14,73 @@ module CodeFormatter
14
14
  @path = path
15
15
  end
16
16
 
17
- def format(*)
17
+ def format(force_all = false)
18
+ return unless commands_exist?
19
+
20
+ files = files_to_format(force_all)
21
+ if files.empty?
22
+ puts "[#{shell_command}]: everything is up-to-date"
23
+ else
24
+ puts "[#{shell_command}]: going to format files:\n#{files.join('\n')}"
25
+ format_files(files)
26
+ end
27
+ end
28
+
29
+ def format_files(*)
18
30
  raise NotImplementedError
19
31
  end
32
+
33
+ def shell_command
34
+ raise NotImplementedError
35
+ end
36
+
37
+ def files_filter
38
+ raise NotImplementedError
39
+ end
40
+
41
+ def commands_exist?
42
+ unless Environment.exist? 'git'
43
+ warn 'warning: failed to find git command'
44
+ return false
45
+ end
46
+
47
+ unless Environment.exist? shell_command
48
+ warn "warning: [#{shell_command}] not found (to install it run 'brew install #{shell_command}')"
49
+ return false
50
+ end
51
+
52
+ true
53
+ end
54
+
55
+ def files_to_format(force_all)
56
+ files = force_all ? all_files : modified_and_staged_files
57
+ files.lines.uniq.map { |f| File.expand_path f.strip }.select { |f| format_file? f }
58
+ end
59
+
60
+ def all_files
61
+ `git ls-files -- #{files_filter}`
62
+ end
63
+
64
+ def modified_and_staged_files
65
+ modified_and_untracked = `git ls-files --others --exclude-standard -m -- #{files_filter}`
66
+ staged = `git diff --cached --name-only -- #{files_filter}`
67
+ modified_and_untracked + staged
68
+ end
69
+
70
+ def format_file?(file)
71
+ return false unless File.file?(file) && File.exist?(file)
72
+ return false if excluded_file?(file)
73
+ included_file?(file)
74
+ end
75
+
76
+ def included_file?(file)
77
+ included_files = config.included_files.map { |f| File.expand_path(f, path) }
78
+ included_files.any? { |f| file.start_with?(f) }
79
+ end
80
+
81
+ def excluded_file?(file)
82
+ excluded_files = config.excluded_files.map { |f| File.expand_path(f, path) }
83
+ excluded_files.any? { |f| file.start_with?(f) }
84
+ end
20
85
  end
21
86
  end
@@ -5,68 +5,21 @@ require 'yaml'
5
5
 
6
6
  module CodeFormatter
7
7
  class SwiftFormatFormatter < Formatter
8
- def format(force_all = false)
9
- unless Environment.exist? 'git'
10
- warn 'warning: failed to find git command'
11
- return
12
- end
13
-
14
- unless Environment.exist? 'swiftformat'
15
- warn "warning: [swiftformat] not found (to install it run 'brew install swiftformat')"
16
- return
17
- end
18
-
19
- files = files_to_format(force_all)
20
- if files.empty?
21
- puts '[swiftformat]: everything is up-to-date'
22
- else
23
- puts "[swiftformat]: going to format files:\n#{files.join('\n')}"
24
- format_files(files)
25
- end
26
- end
27
-
28
- private
29
-
30
- def files_to_format(force_all)
31
- files = force_all ? all_files : modified_and_staged_files
32
- files.lines.uniq.map { |f| File.expand_path f.strip }.select { |f| format_file? f }
33
- end
34
-
35
- def all_files
36
- `git ls-files -- #{files_filter}`
8
+ def format_files(files)
9
+ disabled = disabled_rules.join(',')
10
+ enabled = enabled_rules.join(',')
11
+ `swiftformat #{files.join(' ')} --disable #{disabled} --enable #{enabled}`
37
12
  end
38
13
 
39
- def modified_and_staged_files
40
- modified_and_untracked = `git ls-files --others --exclude-standard -m -- #{files_filter}`
41
- staged = `git diff --cached --name-only -- #{files_filter}`
42
- modified_and_untracked + staged
14
+ def shell_command
15
+ 'swiftformat'
43
16
  end
44
17
 
45
18
  def files_filter
46
19
  "'*.swift'"
47
20
  end
48
21
 
49
- def format_file?(file)
50
- return false unless File.file?(file) && File.exist?(file)
51
- return false if excluded_file?(file)
52
- included_file?(file)
53
- end
54
-
55
- def included_file?(file)
56
- included_files = config.included_files.map { |f| File.expand_path(f, path) }
57
- included_files.any? { |f| file.start_with?(f) }
58
- end
59
-
60
- def excluded_file?(file)
61
- excluded_files = config.excluded_files.map { |f| File.expand_path(f, path) }
62
- excluded_files.any? { |f| file.start_with?(f) }
63
- end
64
-
65
- def format_files(files)
66
- disabled = disabled_rules.join(',')
67
- enabled = enabled_rules.join(',')
68
- `swiftformat #{files.join(' ')} --disable #{disabled} --enable #{enabled}`
69
- end
22
+ private
70
23
 
71
24
  # @return [[String]] enabled rules
72
25
  def enabled_rules
@@ -6,18 +6,21 @@ require 'tempfile'
6
6
 
7
7
  module CodeFormatter
8
8
  class SwiftLintFormatter < Formatter
9
- def format(*)
10
- unless Environment.exist? 'swiftlint'
11
- warn "warning: [swiftlint] not found (to install it run 'brew install swiftlint')"
12
- return
13
- end
14
-
9
+ def format_files(_)
15
10
  create_config do |file|
16
11
  system("swiftlint autocorrect --path #{path} --config #{file.path}")
17
12
  system("swiftlint --path #{path} --config #{file.path}")
18
13
  end
19
14
  end
20
15
 
16
+ def shell_command
17
+ 'swiftlint'
18
+ end
19
+
20
+ def files_filter
21
+ "'*.swift'"
22
+ end
23
+
21
24
  private
22
25
 
23
26
  def create_config
@@ -0,0 +1,12 @@
1
+ BasedOnStyle : WebKit
2
+ BreakBeforeBraces : Attach
3
+ BinPackArguments: false
4
+ BinPackParameters: false
5
+ AllowAllParametersOfDeclarationOnNextLine: false
6
+ SpacesInSquareBrackets : false
7
+ PointerAlignment : Right
8
+ PenaltyReturnTypeOnItsOwnLine : 300
9
+ ColumnLimit : 0
10
+ AllowShortBlocksOnASingleLine: true
11
+ AlignTrailingComments: true
12
+ AllowShortFunctionsOnASingleLine: false
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module CodeFormatter
4
- VERSION = '1.0.6'
4
+ VERSION = '1.1.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: code-formatter
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.6
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - sroik
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-01-22 00:00:00.000000000 Z
11
+ date: 2018-02-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -107,10 +107,12 @@ files:
107
107
  - lib/code_formatter/cli.rb
108
108
  - lib/code_formatter/configuration.rb
109
109
  - lib/code_formatter/environment.rb
110
+ - lib/code_formatter/formatters/clang_format_formatter.rb
110
111
  - lib/code_formatter/formatters/formatter.rb
111
112
  - lib/code_formatter/formatters/swift_format_formatter.rb
112
113
  - lib/code_formatter/formatters/swift_lint_formatter.rb
113
114
  - lib/code_formatter/initializer.rb
115
+ - lib/code_formatter/resources/clang_format_style.yml
114
116
  - lib/code_formatter/resources/sample_config.config
115
117
  - lib/code_formatter/resources/swiftformat_rules.yml
116
118
  - lib/code_formatter/resources/swiftlint.yml