jekyll-pre-commit 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
+ SHA1:
3
+ metadata.gz: ba0866d2499fbb528292ed308379b106d0fdab53
4
+ data.tar.gz: ceb1d3b4cb328cecbfe37fb562dd228ab082af83
5
+ SHA512:
6
+ metadata.gz: 24773f04d3fdbdc8528969df8057e6a695e822366d40fa04ed995472f071af191f37ccceff70a6fdee92dde7ddd4c835bcd9b75febcfcfefb4ee10eda5694582
7
+ data.tar.gz: 080f9210aee062769c4f7f1500452a5b77b4dafd73b416eb9490ae989f518b492285bb45fbc4ba17b45dc18a57102381aacfb1093aac81a0757fb104db50865f
data/.editorconfig ADDED
@@ -0,0 +1,16 @@
1
+ # EditorConfig is awesome: http://EditorConfig.org
2
+
3
+ # top-most EditorConfig file
4
+ root = true
5
+
6
+ [*]
7
+ end_of_line = lf
8
+ insert_final_newline = true
9
+
10
+ [*.rb]
11
+ indent_style = space
12
+ indent_size = 2
13
+
14
+ [pre-commit]
15
+ indent_style = space
16
+ indent_size = 2
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in jekyll-pre-commit.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Max Chadwick
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,115 @@
1
+ # jekyll-pre-commit
2
+
3
+ A Jekyll plugin to make sure your post is _really_ ready for publishing.
4
+
5
+ ![Terminal showing failed check](http://i.imgur.com/9PzDMiB.jpg)
6
+
7
+ ## Installation
8
+
9
+ Add this line to your `Gemfile`:
10
+
11
+ ```ruby
12
+ group :jekyll_plugins do
13
+ gem 'jekyll-migrate-permalink'
14
+ end
15
+ ```
16
+
17
+ Then execute the `bundle` command to install the gem.
18
+
19
+ Next run `bundle exec jekyll pre-commit init` in the root of your Jekyll site.
20
+
21
+ This will symlink this plugin's `pre-commit` file to the `.git/hooks/` directory of your project.
22
+
23
+ If you provide the `--force` flag when running `bundle exec jekyll pre-commit init` any existing `pre-commit` file will be deleted.
24
+
25
+ ## Usage
26
+
27
+ Once installed you may choose the pre-commit checks you would like to use by listing them in your site's `_config.yml`.
28
+
29
+ ```yaml
30
+ pre-commit:
31
+ - check: FrontMatterVariableExists
32
+ variables: ['description', 'image']
33
+ - check: FrontMatterVariableIsNotDuplicate
34
+ variables: ['description']
35
+ - check: FrontMatterVariableMeetsLengthRequirements
36
+ variables: ['description', 'title']
37
+ ```
38
+
39
+ ## Available Checks
40
+
41
+ #### FrontMatterVariableExists
42
+
43
+ This check ensures that any listed variables exist in the front matter of any post that is staged to be committed.
44
+
45
+ #### FrontMatterVariableIsNotDuplicate
46
+
47
+ This check ensures that any listed variables are unique amongst all the posts on your site in the front matter of any post that is staged to be committed.
48
+
49
+ #### FrontMatterVariableMeetsLengthRequirements
50
+
51
+ This check ensures that any listed variables meet the length requirements (in number of characters) in the front matter of any post that is staged to be committed.
52
+
53
+ This check includes the following defaults:
54
+
55
+ **title**
56
+
57
+ - max: 59
58
+
59
+ **description**
60
+
61
+ - min: 145
62
+ - max: 165
63
+
64
+ These can be overridden, or requirements can be specified for other variables in the following format...
65
+
66
+ - `variable|min|max`
67
+
68
+ For example...
69
+
70
+ ```yaml
71
+ - check: FrontMatterVariableMeetsLengthRequirements
72
+ variables: ['title||50']
73
+ ```
74
+
75
+ In the above, there would be a maximum length of 50 characters for the title (rather than the default of 59)
76
+
77
+ ## Roll Your Own
78
+
79
+ You can also add your own checks. To do so, create a class in the `Jekyll::PreCommit::Checks` module and and define a `check` method. Your class should extend the `Jekyll::PreCommit::Checks::Check` class and return the `@result` instance variable.
80
+
81
+ For example...
82
+
83
+ ```ruby
84
+ module Jekyll
85
+ module PreCommit
86
+ module Checks
87
+ class DoesNothing < Check
88
+ def check(staged, not_staged, site, args)
89
+ @result
90
+ end
91
+ end
92
+ end
93
+ end
94
+ end
95
+ ```
96
+
97
+ Put this file in your plugins_path (which is _plugins by default) and `jekyll-pre-commit` will load it automatically. Then just specify that you'd like to run this check in your front matter.
98
+
99
+ ```yaml
100
+ pre-commit:
101
+ - check: DoesNothing
102
+ ```
103
+
104
+ As you can probably tell by the name, this check doesn't actually do anything. Review the checks in `lib/jekyll-pre-commit/checks` for some more useful examples.
105
+
106
+ ## Contributing
107
+
108
+ Bug reports and pull requests are welcome on GitHub at https://github.com/mpchadwick/jekyll-pre-commit.
109
+
110
+ Please ensure tests pass (using rspec) before submitting a pull request and provide test coverage for any new features.
111
+
112
+ ## License
113
+
114
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
115
+
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'jekyll-pre-commit/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "jekyll-pre-commit"
8
+ spec.version = Jekyll::PreCommit::VERSION
9
+ spec.authors = ["Max Chadwick"]
10
+ spec.email = ["mpchadwick@gmail.com"]
11
+
12
+ spec.summary = %q{A Jekyll plugin to make sure your post is _really_ ready for publishing}
13
+ spec.homepage = "https://github.com/mpchadwick/jekyll-pre-commit"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.10"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ end
@@ -0,0 +1,11 @@
1
+ module Jekyll
2
+ module PreCommit
3
+ module Checks
4
+ class Check
5
+ def initialize
6
+ @result = { :ok => true, :message => "" }
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,25 @@
1
+ module Jekyll
2
+ module PreCommit
3
+ module Checks
4
+ class FrontMatterVariableExists < Check
5
+ def check(staged, not_staged, site, args)
6
+ if !args["variables"]
7
+ @result[:message] += "No variables to check."
8
+ return @result
9
+ end
10
+
11
+ staged.each do |post|
12
+ args["variables"].each do |variable|
13
+ if !post.data[variable]
14
+ @result[:ok] = false
15
+ @result[:message] += "#{post.data["title"]} was missing a #{variable}. "
16
+ end
17
+ end
18
+ end
19
+
20
+ @result
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,35 @@
1
+ module Jekyll
2
+ module PreCommit
3
+ module Checks
4
+ class FrontMatterVariableIsNotDuplicate < Check
5
+ def check(staged, not_staged, site, args)
6
+ if !args["variables"]
7
+ @result[:message] += "No variables to check."
8
+ return @result
9
+ end
10
+
11
+ existing = Hash.new
12
+ not_staged.each do |post|
13
+ args["variables"].each do |variable|
14
+ if !existing[variable]
15
+ existing[variable] = Array.new
16
+ end
17
+ existing[variable].push(post.data[variable]) if post.data[variable]
18
+ end
19
+ end
20
+
21
+ staged.each do |post|
22
+ args["variables"].each do |variable|
23
+ if existing[variable].include? post.data[variable]
24
+ @result[:ok] = false
25
+ @result[:message] += "#{post.data["title"]}'s #{variable} was already used. "
26
+ end
27
+ end
28
+ end
29
+
30
+ @result
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,54 @@
1
+ module Jekyll
2
+ module PreCommit
3
+ module Checks
4
+ class FrontMatterVariableMeetsLengthRequirements < Check
5
+
6
+ DEFAULT_LENGTH_REQUIREMENTS = {
7
+ "description" => {
8
+ "min" => 145,
9
+ "max" => 165,
10
+ },
11
+ "title" => {
12
+ "max" => 59
13
+ }
14
+ }
15
+
16
+ def check(staged, not_staged, site, args)
17
+ if !args["variables"]
18
+ @result[:message] += "No variables to check."
19
+ return @result
20
+ end
21
+
22
+ staged.each do |post|
23
+ args["variables"].each do |variable|
24
+ parts = variable.split('|')
25
+ next if !post.data[parts[0]]
26
+ # If use custom configuration if provided
27
+ if parts[1]
28
+ min = parts[1].to_i
29
+ max = parts[2].to_i
30
+ else
31
+ if DEFAULT_LENGTH_REQUIREMENTS[variable] && DEFAULT_LENGTH_REQUIREMENTS[variable]["min"]
32
+ min = DEFAULT_LENGTH_REQUIREMENTS[variable]["min"]
33
+ end
34
+ if DEFAULT_LENGTH_REQUIREMENTS[variable] && DEFAULT_LENGTH_REQUIREMENTS[variable]["max"]
35
+ max = DEFAULT_LENGTH_REQUIREMENTS[variable]["max"]
36
+ end
37
+ end
38
+
39
+ if min && post.data[parts[0]].length < min
40
+ @result[:ok] = false
41
+ @result[:message] += "#{post.data["title"]}'s #{parts[0]} is too short. "
42
+ elsif max && post.data[parts[0]].length > max
43
+ @result[:ok] = false
44
+ @result[:message] += "#{post.data["title"]}'s #{parts[0]} is too long. "
45
+ end
46
+ end
47
+ end
48
+
49
+ @result
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,37 @@
1
+ module Jekyll
2
+ module Commands
3
+ class PreCommit < Command
4
+ class << self
5
+ def init_with_program(prog)
6
+ prog.command(:"pre-commit") do |c|
7
+ c.syntax 'pre-commit SUBCOMMAND'
8
+ c.description 'Runs SUBCOMMAND'
9
+ c.option "force", "--force", "Overwrites an existing pre-commit if one already exists"
10
+
11
+ c.action do |args, options|
12
+ raise ArgumentError, "You must provide a subcommand" if args.empty?
13
+ if args.include? "init"
14
+ dest = "#{Dir.pwd}/.git/hooks/pre-commit"
15
+ if (File.exist?(dest) && !options["force"])
16
+ Jekyll.logger.abort_with "An existing pre-commit file already exists. If you'd like" \
17
+ " to overwrite that file (e.g. you're upgrading) pass the --force flag."
18
+ end
19
+
20
+ if (File.exist?(dest))
21
+ File.delete(dest)
22
+ end
23
+
24
+ # Is there a better way to resolve this path?
25
+ src = `bundle show jekyll-pre-commit`
26
+ src = src.strip + "/lib/jekyll-pre-commit/pre-commit"
27
+
28
+ File.symlink src, dest
29
+ puts "pre-commit hook file created!"
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,33 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $VERBOSE = nil
4
+
5
+ require "jekyll"
6
+
7
+ Jekyll.logger.adjust_verbosity(
8
+ :quiet => true
9
+ )
10
+
11
+ Jekyll::PluginManager.require_from_bundler
12
+
13
+ config = Jekyll.configuration
14
+ config["future"] = true
15
+
16
+ site = Jekyll::Site.new(config)
17
+ site.reset
18
+ site.read
19
+
20
+ staged_files = `git diff --cached --name-only --diff-filter=ACM`.split("\n")
21
+
22
+ runner = Jekyll::PreCommit::Runner.new
23
+ result = runner.run(site, staged_files)
24
+
25
+ if result[:ok]
26
+ exit(0)
27
+ end
28
+
29
+ puts "There were failed checks 😥"
30
+ result[:messages].each do |c|
31
+ puts c
32
+ end
33
+ exit(1)
@@ -0,0 +1,51 @@
1
+ module Jekyll
2
+ module PreCommit
3
+ class Runner
4
+
5
+ def run(site, staged_files)
6
+ result = { :ok => true, :messages => [] }
7
+ if !site.config["pre-commit"] || site.config["pre-commit"].empty?
8
+ # Bail if there are no pre-commit checks enabled
9
+ result[:messages].push("No pre-commit checks enabled")
10
+ return result
11
+ end
12
+
13
+ staged_posts = Array.new
14
+ not_staged_posts = Array.new
15
+
16
+ site.posts.docs.each do |p|
17
+ if (staged_files.include? p.path.gsub(Dir.pwd + "/", ""))
18
+ staged_posts.push(p)
19
+ else
20
+ not_staged_posts.push(p)
21
+ end
22
+ end
23
+
24
+ if staged_posts.empty?
25
+ # If no posts are being committed. Safe to commit.
26
+ result[:messages].push("No posts staged")
27
+ return result
28
+ end
29
+
30
+ site.config["pre-commit"].each do |c|
31
+ begin
32
+ o = Object.const_get("Jekyll::PreCommit::Checks::" + c["check"]).new
33
+ rescue
34
+ result[:ok] = false
35
+ result[:messages].push("The check #{c["check"]} does not exist! Please fix your configuration.")
36
+ break
37
+ end
38
+ r = o.check(staged_posts, not_staged_posts, site, c)
39
+ if !r[:ok]
40
+ result[:ok] = false
41
+ end
42
+ if r[:message] != ""
43
+ result[:messages].push(r[:message])
44
+ end
45
+ end
46
+
47
+ return result
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,5 @@
1
+ module Jekyll
2
+ module PreCommit
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,12 @@
1
+ module Jekyll
2
+ module PreCommit
3
+ autoload :VERSION, 'jekyll-pre-commit/version.rb'
4
+ end
5
+ end
6
+
7
+ require 'jekyll-pre-commit/runner.rb'
8
+ require 'jekyll-pre-commit/commands.rb'
9
+ require 'jekyll-pre-commit/checks/check.rb'
10
+ require 'jekyll-pre-commit/checks/front_matter_variable_exists.rb'
11
+ require 'jekyll-pre-commit/checks/front_matter_variable_is_not_duplicate.rb'
12
+ require 'jekyll-pre-commit/checks/front_matter_variable_meets_length_requirements.rb'
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jekyll-pre-commit
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Max Chadwick
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-01-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description:
42
+ email:
43
+ - mpchadwick@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .editorconfig
49
+ - .gitignore
50
+ - .rspec
51
+ - Gemfile
52
+ - LICENSE.txt
53
+ - README.md
54
+ - Rakefile
55
+ - jekyll-pre-commit.gemspec
56
+ - lib/jekyll-pre-commit.rb
57
+ - lib/jekyll-pre-commit/checks/check.rb
58
+ - lib/jekyll-pre-commit/checks/front_matter_variable_exists.rb
59
+ - lib/jekyll-pre-commit/checks/front_matter_variable_is_not_duplicate.rb
60
+ - lib/jekyll-pre-commit/checks/front_matter_variable_meets_length_requirements.rb
61
+ - lib/jekyll-pre-commit/commands.rb
62
+ - lib/jekyll-pre-commit/pre-commit
63
+ - lib/jekyll-pre-commit/runner.rb
64
+ - lib/jekyll-pre-commit/version.rb
65
+ homepage: https://github.com/mpchadwick/jekyll-pre-commit
66
+ licenses:
67
+ - MIT
68
+ metadata: {}
69
+ post_install_message:
70
+ rdoc_options: []
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubyforge_project:
85
+ rubygems_version: 2.0.14.1
86
+ signing_key:
87
+ specification_version: 4
88
+ summary: A Jekyll plugin to make sure your post is _really_ ready for publishing
89
+ test_files: []