immosquare-cleaner 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 99d7d3985550b02c0e29de7fe0934b9b3190f82961570d671fbc45a4fdfe8f17
4
+ data.tar.gz: 655b87bc65f6b485050f8ee616ecb8601d8b130d619bdfe9856ce3b8a8d5c280
5
+ SHA512:
6
+ metadata.gz: 8e01de397011a5a527e8fcd3f931ca4b66b7d3851d52315f10a58e7d18b8da79c8d82f71cddc19f2af7933916bf91e2464751196ee6880e2f8590611c485a623
7
+ data.tar.gz: 41b6a114cb2c420227afe547b3cccfb6f350afb19c8202a4ce7453348a4438b45ffbebcb0fcf11e934845324b87a72e1d141ab5e8a4861df1479c1349df51518
@@ -0,0 +1,53 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "immosquare_cleaner"
4
+ require "optparse"
5
+
6
+ options = {}
7
+
8
+ ##===========================================================================##
9
+ ## optparse is a standard library of ruby, it's used to parse command line ##
10
+ ##===========================================================================##
11
+ OptionParser.new do |opts|
12
+ opts.banner = "Usage: immosquare-cleaner [options] file"
13
+
14
+ ##===========================================================================##
15
+ ## Capture all single-letter options (like '-t', '-a', etc.) ##
16
+ ##===========================================================================##
17
+ ("a".."z").each do |letter|
18
+ opts.on("-#{letter} [OPTION]", "Option -#{letter}") do |value|
19
+ options[letter.to_sym] = value
20
+ end
21
+ end
22
+
23
+ ##===========================================================================##
24
+ ## If you also want to capture longer options, add them here ##
25
+ ## Example: ##
26
+ ## opts.on("--tag TAG", "Tag option") do |t| ##
27
+ ## options[:tag] = t ##
28
+ ## end ##
29
+ ##===========================================================================##
30
+ end.parse!
31
+
32
+ ##===========================================================================##
33
+ ## Check if the file path is provided ##
34
+ ##===========================================================================##
35
+ file_path = ARGV[0]
36
+ if !file_path
37
+ puts("Error: Please provide a file path.")
38
+ exit 1
39
+ end
40
+
41
+ ##===========================================================================##
42
+ ## Check if the file exists ##
43
+ ##===========================================================================##
44
+ if !File.exist?(file_path)
45
+ puts("Error: The file '#{file_path}' does not exist.")
46
+ exit 1
47
+ end
48
+
49
+
50
+ ##===========================================================================##
51
+ ## We can now call the clean method, passing the file path and options ##
52
+ ##===========================================================================##
53
+ ImmosquareCleaner.clean(file_path, **options)
@@ -0,0 +1,16 @@
1
+ module ImmosquareCleaner
2
+ class Configuration
3
+
4
+ attr_accessor :rubocop_options, :htmlbeautifier_options, :erblint_options
5
+
6
+ def initialize
7
+ @rubocop_options = nil
8
+ @htmlbeautifier_options = nil
9
+ @erblint_options = nil
10
+ end
11
+
12
+
13
+
14
+
15
+ end
16
+ end
@@ -0,0 +1,9 @@
1
+ module ImmosquareYaml
2
+ class Railtie < Rails::Railtie
3
+
4
+ rake_tasks do
5
+ load "tasks/immosquare-cleaner.rake"
6
+ end
7
+
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module ImmosquareCleaner
2
+ VERSION = "0.1.0".freeze
3
+ end
@@ -0,0 +1,136 @@
1
+ require "immosquare-yaml"
2
+ require_relative "immosquare-cleaner/configuration"
3
+ require_relative "immosquare-yaml/railtie" if defined?(Rails)
4
+
5
+ module ImmosquareCleaner
6
+ class << self
7
+
8
+ ##===========================================================================##
9
+ ## Constants
10
+ ##===========================================================================##
11
+ SHEBANG = "#!/usr/bin/env ruby".freeze
12
+
13
+ ##===========================================================================##
14
+ ## Gem configuration
15
+ ##===========================================================================##
16
+ attr_writer :configuration
17
+
18
+ def configuration
19
+ @configuration ||= Configuration.new
20
+ end
21
+
22
+ def config
23
+ yield(configuration)
24
+ end
25
+
26
+ def clean(file_path, **options)
27
+ ##============================================================##
28
+ ## Default options
29
+ ##============================================================##
30
+ cmd = []
31
+ options = {}.merge(options)
32
+
33
+
34
+ begin
35
+ raise("Error: The file '#{file_path}' does not exist.") if !File.exist?(file_path)
36
+
37
+ ##============================================================##
38
+ ## We normalize the last line of the file to ensure it ends with a single
39
+ ##============================================================##
40
+ normalize_last_line(file_path)
41
+
42
+ ##============================================================##
43
+ ## We clean files based on their extension
44
+ ##============================================================##
45
+ if file_path.end_with?(".html.erb")
46
+ cmd << [true, "bundle exec htmlbeautifier #{file_path} #{ImmosquareCleaner.configuration.htmlbeautifier_options || "--keep-blank-lines 4"}"]
47
+ cmd << [true, "bundle exec erblint -c #{gem_root}/linters/erb-lint.yml #{file_path} #{ImmosquareCleaner.configuration.erblint_options || "--autocorrect"}"]
48
+ elsif file_path.end_with?(".rb", ".rake", "Gemfile", "Rakefile", ".axlsx", ".gemspec", ".ru", ".podspec", ".jbuilder", ".rabl", ".thor", "config.ru", "Berksfile", "Capfile", "Guardfile", "Podfile", "Thorfile", "Vagrantfile") || File.open(file_path, &:gets)&.include?(SHEBANG)
49
+ cmd << [true, "bundle exec rubocop -c #{gem_root}/linters/rubocop.yml #{file_path} #{ImmosquareCleaner.configuration.rubocop_options || "--autocorrect-all"}"]
50
+ elsif file_path =~ %r{locales/.*\.yml$}
51
+ ImmosquareYaml.clean(file_path)
52
+ elsif npx_installed? && prettier_installed?
53
+ prettier_parser = nil
54
+ prettier_parser = "--parser markdown" if file_path.end_with?(".md.erb")
55
+ cmd << [false, "npx prettier --write #{file_path} #{prettier_parser} --config #{gem_root}/linters/prettier.yml"]
56
+ else
57
+ puts("Warning: npx and/or prettier are not installed. Skipping formatting.")
58
+ end
59
+
60
+
61
+
62
+ ##===========================================================================##
63
+ ## We change the current directory to the gem root to ensure the gem's paths
64
+ ## are used when executing the commands
65
+ ##===========================================================================##
66
+ cmd.each do |from_gem_root, c|
67
+ if from_gem_root
68
+ Dir.chdir(gem_root) { system(c) }
69
+ else
70
+ system(c)
71
+ end
72
+ end if !cmd.empty?
73
+ rescue StandardError => e
74
+ puts(e.message)
75
+ false
76
+ end
77
+ end
78
+
79
+
80
+ private
81
+
82
+ def gem_root
83
+ File.expand_path("..", __dir__)
84
+ end
85
+
86
+ def npx_installed?
87
+ system("which npx > /dev/null 2>&1")
88
+ end
89
+
90
+ def prettier_installed?
91
+ system("npx prettier --version > /dev/null 2>&1")
92
+ end
93
+
94
+ ##===========================================================================##
95
+ ## This method ensures the file ends with a single newline, facilitating
96
+ ## cleaner multi-line blocks. It operates by reading all lines of the file,
97
+ ## removing any empty lines at the end, and then appending a newline.
98
+ ## This guarantees the presence of a newline at the end, and also prevents
99
+ ## multiple newlines from being present at the end.
100
+ ##
101
+ ## Params:
102
+ ## +file_path+:: The path to the file to be normalized.
103
+ ##
104
+ ## Returns:
105
+ ## The total number of lines in the normalized file.
106
+ ##===========================================================================##
107
+ def normalize_last_line(file_path)
108
+ ##============================================================##
109
+ ## Read all lines from the file
110
+ ## https://gist.github.com/guilhermesimoes/d69e547884e556c3dc95
111
+ ##============================================================##
112
+ content = File.read(file_path)
113
+
114
+ ##===========================================================================##
115
+ ## Remove all trailing empty lines at the end of the file
116
+ content.gsub!(/#{Regexp.escape($INPUT_RECORD_SEPARATOR)}+\z/, "")
117
+ ##===========================================================================##
118
+
119
+ ##===========================================================================##
120
+ ## Append a newline at the end to maintain the file structure
121
+ ###===========================================================================##
122
+ content += $INPUT_RECORD_SEPARATOR
123
+
124
+ ##===========================================================================##
125
+ ## Write the modified lines back to the file
126
+ ##===========================================================================##
127
+ File.write(file_path, content)
128
+
129
+ ##===========================================================================##
130
+ ## Return the total number of lines in the modified file
131
+ ##===========================================================================##
132
+ content.lines.size
133
+ end
134
+
135
+ end
136
+ end
@@ -0,0 +1,11 @@
1
+ namespace :immosquare_cleaner do
2
+ ##============================================================##
3
+ ## Function to clean translation files in rails app
4
+ ##============================================================##
5
+ desc "Clean translation files in rails app"
6
+ task :clean => :environment do
7
+ Dir.glob("#{Rails.root}/*").each do |file|
8
+ ImmosquareCleaner.clean(file)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,13 @@
1
+ EnableDefaultLinters: true
2
+ linters:
3
+ NoJavascriptTagHelper:
4
+ enabled: false
5
+ SpaceInHtmlTag:
6
+ enabled: false
7
+ Rubocop:
8
+ enabled: true
9
+ rubocop_config:
10
+ inherit_from:
11
+ - linters/rubocop.yml
12
+ Layout/TrailingWhitespace:
13
+ Enabled: false
@@ -0,0 +1,12 @@
1
+ # https://www.prettier.cn/docs/options.html
2
+ printWidth: 10000
3
+ tabWidth: 2
4
+ useTabs: false
5
+ semi: false
6
+ singleQuote: false
7
+ quoteProps: "as-needed"
8
+ trailingComma: "none"
9
+ bracketSpacing: true
10
+ arrowParens: "always"
11
+ endOfLine: "lf"
12
+ singleAttributePerLine: false
@@ -0,0 +1,2 @@
1
+ inherit_gem:
2
+ immosquare-rubocop: rubocop.yml
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: immosquare-cleaner
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - IMMO SQUARE
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-09-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: erb_lint
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: htmlbeautifier
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: immosquare-rubocop
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: immosquare-yaml
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Immosquare-cleaner streamlines Rails applications by running tools like
70
+ RuboCop, ERBLint, Stylelint and more. It ensures code quality, readability, and
71
+ consistency across the application.
72
+ email:
73
+ - jules@immosquare.com
74
+ executables:
75
+ - immosquare-cleaner
76
+ extensions: []
77
+ extra_rdoc_files: []
78
+ files:
79
+ - bin/immosquare-cleaner
80
+ - lib/immosquare-cleaner/configuration.rb
81
+ - lib/immosquare-cleaner/railtie.rb
82
+ - lib/immosquare-cleaner/version.rb
83
+ - lib/immosquare_cleaner.rb
84
+ - lib/tasks/immosquare-cleaner.rake
85
+ - linters/erb-lint.yml
86
+ - linters/prettier.yml
87
+ - linters/rubocop.yml
88
+ homepage: https://github.com/IMMOSQUARE/Immosquare-cleaner
89
+ licenses:
90
+ - MIT
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ - linters
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: 2.7.2
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubygems_version: 3.4.13
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: A gem to lint and organize files in a Rails application.
112
+ test_files: []