tagrity 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 0ee6568e2b6eece64b41ea5bfd180aff6770f909d2ae0a2d1b113d6776913bf7
4
+ data.tar.gz: 963d36ade8dcae0fcf5f9457c057c2ca99984c0e6c27ca5d583bfee960b0fd0e
5
+ SHA512:
6
+ metadata.gz: 07b68faab753df68dd50a90c0059498b35f55de439fd228e04a93d246489b3db50073d148c644bce12007d2c119bd6deaa9c3a17156d2680fa36f3f87aeea26e
7
+ data.tar.gz: 73d254a884b320d83fa06c794a7e30940b29d7021915ae8c4374f5d34c675a9af2a2d25c55bf42f37704e93dbba49263d0e0bcc2ca14c82e46bc6709204a836c
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ /var/
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.3
7
+ before_install: gem install bundler -v 2.0.2
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at rethy.spud@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in tagrity.gemspec
4
+ gemspec
@@ -0,0 +1,47 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ tagrity (0.1.3)
5
+ listen (~> 3.0)
6
+ thor (~> 0.20)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ diff-lcs (1.3)
12
+ ffi (1.11.3)
13
+ listen (3.2.0)
14
+ rb-fsevent (~> 0.10, >= 0.10.3)
15
+ rb-inotify (~> 0.9, >= 0.9.10)
16
+ rake (10.5.0)
17
+ rb-fsevent (0.10.3)
18
+ rb-inotify (0.10.0)
19
+ ffi (~> 1.0)
20
+ ripper-tags (0.8.0)
21
+ rspec (3.9.0)
22
+ rspec-core (~> 3.9.0)
23
+ rspec-expectations (~> 3.9.0)
24
+ rspec-mocks (~> 3.9.0)
25
+ rspec-core (3.9.0)
26
+ rspec-support (~> 3.9.0)
27
+ rspec-expectations (3.9.0)
28
+ diff-lcs (>= 1.2.0, < 2.0)
29
+ rspec-support (~> 3.9.0)
30
+ rspec-mocks (3.9.0)
31
+ diff-lcs (>= 1.2.0, < 2.0)
32
+ rspec-support (~> 3.9.0)
33
+ rspec-support (3.9.0)
34
+ thor (0.20.3)
35
+
36
+ PLATFORMS
37
+ ruby
38
+
39
+ DEPENDENCIES
40
+ bundler (~> 2.0)
41
+ rake (~> 10.0)
42
+ ripper-tags (~> 0.8.0)
43
+ rspec (~> 3.0)
44
+ tagrity!
45
+
46
+ BUNDLED WITH
47
+ 2.0.2
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Adam P. Regasz-Rethy
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.
@@ -0,0 +1,127 @@
1
+ # Tagrity
2
+
3
+ Automatically regenerate tags on file changes.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'tagrity'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install tagrity
20
+
21
+ ## Usage
22
+
23
+ ```
24
+ Commands:
25
+ tagrity help [COMMAND] # Describe available commands or one specific command
26
+ tagrity start # Start watching a directory (default to pwd)
27
+ tagrity status # List running tagrity processes and the directories being watched
28
+ tagrity stop # Stop watching a directory (default to pwd)
29
+ ```
30
+
31
+ ### start
32
+
33
+ ```
34
+ Usage:
35
+ tagrity start
36
+
37
+ Options:
38
+ [--dir=DIR]
39
+ [--fg], [--no-fg]
40
+ [--configfile=CONFIGFILE]
41
+ [--tagf=TAGF]
42
+ [--default-cmd=DEFAULT_CMD]
43
+ [--excluded-exts=one two three]
44
+ [--excluded-paths=one two three]
45
+
46
+ Start watching a directory (default to pwd)
47
+ ```
48
+
49
+ ### stop
50
+
51
+ ```
52
+ Usage:
53
+ tagrity stop
54
+
55
+ Options:
56
+ [--dir=DIR]
57
+
58
+ Stop watching a directory (default to pwd)
59
+ ```
60
+
61
+ ### status
62
+
63
+ ```
64
+ Usage:
65
+ tagrity status
66
+
67
+ List running tagrity processes and the directories being watched
68
+ ```
69
+
70
+ ## Development
71
+
72
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
73
+
74
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
75
+
76
+ ## Contributing
77
+
78
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/tagrity. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
79
+
80
+ ## License
81
+
82
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
83
+
84
+ ## Code of Conduct
85
+
86
+ Everyone interacting in the Tagrity project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/tagrity/blob/master/CODE_OF_CONDUCT.md).
87
+
88
+
89
+
90
+
91
+ tagrity config to use (default to ~/.config/tagrity/config.yml if available).
92
+ A config file is a yaml file with the following possible values.
93
+ Some of these can be overridden with options, however the configfile
94
+ provided via --configfile will override the global config file in
95
+ ~/.config/tagrity/config.yml
96
+ ```
97
+ # ext_cmds allows different tag generators to be used depending on the file extension.
98
+ # Multiple extensions does not work, *.html.erb files will be picked up as erb.
99
+ # ext_cmds:
100
+ # <file extension>: <command to use to generate tags for this file extension>
101
+ # DEFAULT: empty
102
+ ext_cmds:
103
+ rb: ripper-tags
104
+ c: ctags
105
+ go: gotags
106
+
107
+ # default_cmd specifies the default command to be used to generate tags
108
+ # default_cmd: <command>
109
+ # DEFAULT: ctags
110
+ default_cmd: ctags
111
+
112
+ # tagf is the filename (relative) to generate tags into
113
+ # tagf: <filename>
114
+ # DEFAULT: tags
115
+ tagf: tags
116
+
117
+ # excluded_exts specifies which file extensions to not generate tags for.
118
+ # excluded_exts: [<file extension>, ...]
119
+ # DEFAULT: []
120
+ excluded_exts: [rb, h, js]
121
+
122
+ # excluded_paths specifies which paths to ignore.
123
+ # It's usually better to avoid this since by default tagrity will only look
124
+ # at files which are tracked by git.
125
+ # excluded_paths: [<path>, ...]
126
+ # DEFAULT: []
127
+ excluded_paths: [vendor, node_modules]
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "tagrity"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "tagrity/cli"
4
+ Tagrity::CLI.start(ARGV)
File without changes
@@ -0,0 +1,2 @@
1
+ module Tagrity
2
+ end
@@ -0,0 +1,61 @@
1
+ require 'thor'
2
+ require 'tagrity/commands/start'
3
+ require 'tagrity/commands/stop'
4
+ require 'tagrity/commands/status'
5
+
6
+ module Tagrity
7
+ class CLI < Thor
8
+ desc "start", "Start watching a directory (default to pwd)"
9
+ option :dir, desc: "directory to watch (omit to use pwd)"
10
+ option :fresh, type: :boolean, default: false, desc: "index the whole codebase before watching the file system."
11
+ option :tagf, desc: "filename (relative) to generate tags into (default: 'tags')."
12
+ option :git, type: :boolean, default: true, desc: "only index files which are being tracked by git"
13
+ option :fg, type: :boolean, desc: "keep the tagrity process running in the foreground"
14
+ option :configfile, desc: "See README for more info."
15
+ option :default_cmd, desc: "the default command to be used to generate tags (default: 'ctags')"
16
+ option :excluded_exts, type: :array, desc: "which file extensions to not generate tags for."
17
+ option :excluded_paths, type: :array, desc: "which paths to ignore. Usually better to ignore this since by default only file tracked by git are indexed."
18
+ def start()
19
+ setup_config
20
+ Command::Start::call(dir, fg?, fresh?)
21
+ end
22
+
23
+ desc "stop", "Stop watching a directory (default to pwd)"
24
+ option :dir
25
+ def stop()
26
+ Command::Stop::call(dir)
27
+ end
28
+
29
+ desc "status", "List running tagrity processes and the directories being watched"
30
+ def status
31
+ Command::Status::call
32
+ end
33
+
34
+ private
35
+
36
+ def dir
37
+ dir = options[:dir] || Dir.pwd
38
+ raise Errno::ENOENT, "No such directory - #{dir}" unless Dir.exists?(dir)
39
+ dir
40
+ end
41
+
42
+ def fg?
43
+ options[:fg]
44
+ end
45
+
46
+ def fresh?
47
+ options[:fresh]
48
+ end
49
+
50
+ def setup_config
51
+ ConfigFile.instance.init(
52
+ configfile: options[:configfile],
53
+ default_cmd: options[:default_cmd],
54
+ tagf: options[:tagf],
55
+ excluded_exts: options[:excluded_exts],
56
+ excluded_paths: options[:excluded_paths],
57
+ git: options[:git]
58
+ )
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,51 @@
1
+ require 'listen'
2
+ require 'tagrity/pid_file'
3
+ require 'tagrity/helper'
4
+ require 'tagrity/file_callbacks'
5
+ require 'tagrity/provider'
6
+
7
+ module Tagrity
8
+ module Command
9
+ class Start
10
+ class ErrorProcessAlreadyRunning < StandardError; end
11
+
12
+ class << self
13
+ def call(dir, fg, fresh)
14
+ assert_not_running(dir)
15
+
16
+ Process.daemon(nochdir: true) unless fg
17
+
18
+ callbacks = Provider.provide(:file_callbacks)
19
+ PidFile.write(PidFile.new(dir, Process.pid))
20
+
21
+ callbacks.on_fresh if fresh
22
+
23
+ listener = Listen.to(
24
+ dir,
25
+ relative: true,
26
+ ) do |modified, added, removed|
27
+ callbacks.on_files_modified(modified)
28
+ callbacks.on_files_added(added)
29
+ callbacks.on_files_removed(removed)
30
+ end
31
+ listener.start
32
+ sleep
33
+ rescue ErrorProcessAlreadyRunning => e
34
+ puts e.message
35
+ rescue Interrupt => e
36
+ PidFile.delete(dir)
37
+ end
38
+
39
+ private
40
+
41
+ def assert_not_running(dir)
42
+ running_processes = PidFile.alive_pid_files(dir: dir)
43
+ unless running_processes.empty?
44
+ pids = running_processes.map { |pid_file| pid_file.pid }
45
+ raise ErrorProcessAlreadyRunning, "Error: tagrity is already watching #{dir} with process #{pids}"
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,13 @@
1
+ module Tagrity
2
+ module Command
3
+ class Status
4
+ class << self
5
+ def call
6
+ PidFile.alive_pid_files.each do |pid_file|
7
+ puts "#{pid_file.pid} #{pid_file.dir}"
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,19 @@
1
+ module Tagrity
2
+ module Command
3
+ class Stop
4
+ class << self
5
+ def call(dir)
6
+ pid_files = PidFile.alive_pid_files(dir: dir)
7
+ if pid_files.empty?
8
+ puts "😕 tagrity doesn't seem to be watching #{dir}"
9
+ else
10
+ pid_files.each do |pid_file|
11
+ pid_file.delete
12
+ puts "Successfully killed #{pid_file.pid}"
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,110 @@
1
+ require 'yaml'
2
+ require 'singleton'
3
+ require 'tagrity/helper'
4
+
5
+ module Tagrity
6
+ class ConfigFile
7
+ include Singleton
8
+
9
+ class ErrorTagFileNotWritable < StandardError; end
10
+ class ErrorGitNotExecutable < StandardError; end
11
+
12
+ def init(
13
+ configfile:,
14
+ default_cmd:,
15
+ tagf:,
16
+ excluded_exts:,
17
+ excluded_paths:,
18
+ git:
19
+ )
20
+ fname = config_file_name(configfile)
21
+ @config = if fname.nil? then {} else YAML.load_file(fname) end
22
+ ensure_ext_cmds
23
+ ensure_default_cmd(default_cmd)
24
+ ensure_tagf(tagf)
25
+ ensure_excluded_exts(excluded_exts)
26
+ ensure_excluded_paths(excluded_paths)
27
+ ensure_git(git)
28
+ end
29
+
30
+ def ft_to_cmd(ext)
31
+ ft_cmd = @config['ext_cmds'][ext]
32
+ return @config['default_cmd'] if ft_cmd.nil? || !Helper.is_executable?(ft_cmd)
33
+ ft_cmd
34
+ end
35
+
36
+ def is_ft_excluded(ext)
37
+ @config['excluded_exts'].include?(ext)
38
+ end
39
+
40
+ def is_path_excluded(path)
41
+ @config['excluded_paths'].any? { |pat| /^(\.\/)?#{pat}.*/ =~ path }
42
+ end
43
+
44
+ def tagf
45
+ @config['tagf']
46
+ end
47
+
48
+ def respect_git?
49
+ @config['git']
50
+ end
51
+
52
+ def to_s
53
+ @config.to_s
54
+ end
55
+
56
+ private
57
+
58
+ def ensure_ext_cmds
59
+ set_option('ext_cmds', nil, {})
60
+ end
61
+
62
+ def ensure_default_cmd(default_cmd)
63
+ set_option('default_cmd', default_cmd, 'ctags')
64
+ end
65
+
66
+ def ensure_excluded_exts(excluded_exts)
67
+ set_option('excluded_exts', excluded_exts, [])
68
+ end
69
+
70
+ def ensure_excluded_paths(excluded_paths)
71
+ set_option('excluded_paths', excluded_paths, [])
72
+ end
73
+
74
+ def ensure_tagf(tagf)
75
+ set_option('tagf', tagf, 'tags')
76
+ unless File.writable?(@config['tagf'])
77
+ raise ErrorTagFileNotWritable, "#{@config['tagf']} must be writable to be used as the tag file."
78
+ end
79
+ end
80
+
81
+ def ensure_git(git)
82
+ set_option('git', git, true)
83
+ if @config['git'] && !Helper.is_executable?('git')
84
+ raise ErrorGitNotExecutable, "'git' must be executable to use the --git option."
85
+ end
86
+ end
87
+
88
+ def set_option(key, local_val, default)
89
+ unless local_val.nil?
90
+ @config[key] = local_val
91
+ end
92
+ return if @config.key?(key) && !@config[key].nil? && @config[key].is_a?(default.class)
93
+ @config[key] = default
94
+ end
95
+
96
+ def config_file_name(fname)
97
+ return fname unless fname.nil?
98
+ return global_config_file_name if File.file?(global_config_file_name)
99
+ nil
100
+ end
101
+
102
+ def global_config_file_name
103
+ "#{global_config_dir_name}/config.yml"
104
+ end
105
+
106
+ def global_config_dir_name
107
+ "#{ENV["HOME"]}/.config/tagrity"
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,26 @@
1
+ require 'tagrity/tag_generator'
2
+ require 'tagrity/config_file'
3
+
4
+ module Tagrity
5
+ class FileCallbacks
6
+ def initialize(tag_generator)
7
+ @tag_generator = tag_generator
8
+ end
9
+
10
+ def on_fresh
11
+ @tag_generator.generate_all
12
+ end
13
+
14
+ def on_files_modified(files)
15
+ @tag_generator.generate(files)
16
+ end
17
+
18
+ def on_files_added(files)
19
+ @tag_generator.generate(files)
20
+ end
21
+
22
+ def on_files_removed(files)
23
+ @tag_generator.delete_files_tags(files)
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,57 @@
1
+ module Tagrity
2
+ class Helper
3
+ root = ENV['TEST'] ? __dir__ : "#{ENV['HOME']}/.tagrity/"
4
+ RUN_DIR = File.join(root, 'var/run').freeze
5
+ LOG_DIR = File.join(root, 'var/log').freeze
6
+
7
+ class << self
8
+ def run_dir
9
+ ensure_data_dirs
10
+ RUN_DIR
11
+ end
12
+
13
+ def is_executable?(cmd)
14
+ !%x{command -v #{cmd}}.empty?
15
+ end
16
+
17
+ def kill(pid)
18
+ Process.kill('HUP', pid)
19
+ end
20
+
21
+ def alive?(pid)
22
+ Process.kill(0, pid) # signal 0 checks if pid is alive
23
+ true
24
+ rescue Errno::ESRCH
25
+ false
26
+ rescue Errno::EPERM
27
+ true
28
+ end
29
+
30
+ def is_git_dir?
31
+ return @is_git_dir unless @is_git_dir.nil?
32
+ `git rev-parse --git-dir &> /dev/null`
33
+ if $?.exitstatus == 0
34
+ @is_git_dir = true
35
+ else
36
+ @is_git_dir = false
37
+ end
38
+ end
39
+
40
+ def is_file_ignored?(file)
41
+ `git check-ignore -q #{file} &> /dev/null`
42
+ $?.exitstatus == 0
43
+ end
44
+
45
+ def is_file_tracked?(file)
46
+ `git ls-files --error-unmatch #{file} &> /dev/null`
47
+ $?.exitstatus == 0
48
+ end
49
+
50
+ private
51
+
52
+ def ensure_data_dirs
53
+ FileUtils.mkdir_p(RUN_DIR)
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,77 @@
1
+ require 'tagrity/helper'
2
+
3
+ module Tagrity
4
+ class PidFile
5
+ class << self
6
+ def write(pid_file)
7
+ File.write("#{Helper.run_dir}/#{pid_file.name}", pid_file.dir)
8
+ end
9
+
10
+ def delete(dir)
11
+ pid_file_paths = Dir.glob("#{Helper.run_dir}/#{dir.split('/').last}.*.pid").select do |path|
12
+ full_dir = File.read(path)
13
+ File.realdirpath(full_dir) == File.realdirpath(dir)
14
+ end
15
+
16
+ pid_file_paths.each do |path|
17
+ File.delete(path)
18
+ Helper.kill(pid_from_path(path))
19
+ end
20
+ end
21
+
22
+ def alive_pid_files(dir: nil)
23
+ Dir.glob("#{Helper.run_dir}/*").reduce([]) do |pid_files, path|
24
+ pid = pid_from_path(path)
25
+ pid_file_dir = File.read(path)
26
+
27
+ if dir.nil? || is_same_dirs(pid_file_dir, dir)
28
+ if Helper.alive?(pid)
29
+ pid_files << PidFile.new(pid_file_dir, pid)
30
+ else
31
+ File.delete(path)
32
+ end
33
+ end
34
+
35
+ pid_files
36
+ end
37
+ end
38
+
39
+ private
40
+
41
+ def is_same_dirs(dir1, dir2)
42
+ File.realdirpath(dir1) == File.realdirpath(dir2)
43
+ end
44
+
45
+ def pid_from_path(pid_file_name)
46
+ pid_file_name.split('.')[-2].to_i
47
+ end
48
+ end
49
+
50
+ attr_reader :pid, :dir
51
+
52
+ def initialize(dir, pid)
53
+ @dir = dir
54
+ @pid = pid
55
+ end
56
+
57
+ def ==(other)
58
+ @dir == other.dir
59
+ @pid == other.pid
60
+ end
61
+
62
+ def name
63
+ "#{@dir.split('/').last}.#{@pid}.pid"
64
+ end
65
+
66
+ def delete
67
+ File.delete(pid_file_path)
68
+ Helper.kill(pid.to_i)
69
+ end
70
+
71
+ private
72
+
73
+ def pid_file_path
74
+ "#{Helper.run_dir}/#{name}"
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,26 @@
1
+ require 'tagrity/file_callbacks'
2
+ require 'tagrity/config_file'
3
+ require 'tagrity/tag_generator'
4
+
5
+ module Tagrity
6
+ class Provider
7
+ class << self
8
+ def provide(want)
9
+ case want
10
+ when :file_callbacks
11
+ provide_file_callbacks
12
+ when :tag_generator
13
+ provide_tag_generator
14
+ end
15
+ end
16
+
17
+ def provide_file_callbacks
18
+ FileCallbacks.new(provide(:tag_generator))
19
+ end
20
+
21
+ def provide_tag_generator
22
+ TagGenerator.new
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,95 @@
1
+ require 'tmpdir'
2
+ require 'tagrity/helper'
3
+
4
+ module Tagrity
5
+ class TagGenerator
6
+ class ExecutableNonExist < StandardError; end
7
+
8
+ def initialize
9
+ assert_executables
10
+ @config = ConfigFile.instance
11
+ end
12
+
13
+ def generate_all
14
+ if File.exists?(tagf)
15
+ File.delete(tagf)
16
+ end
17
+ if check_git?
18
+ files = `git ls-files 2> /dev/null`.split
19
+ else
20
+ files = `find * 2> /dev/null`.split
21
+ end
22
+ if $?.exitstatus == 0
23
+ generate(files)
24
+ else
25
+ puts "Failed to get a listing of all files under pwd for use with --fresh."
26
+ end
27
+ end
28
+
29
+ def generate(files)
30
+ return if files.empty?
31
+ files
32
+ .select { |file| generate_tags?(file) }
33
+ .group_by { |file| @config.ft_to_cmd(file.partition('.').last) }
34
+ .each do |cmd, fnames|
35
+ `#{cmd} -f #{tagf} --append #{fnames.join(' ')} &> /dev/null`
36
+ if $?.exitstatus == 0
37
+ puts "{#{cmd}} generated tags for #{fnames} into #{tagf}"
38
+ else
39
+ puts "{#{cmd}} failed to generate tags for #{fnames} into #{tagf}"
40
+ end
41
+ end
42
+ end
43
+
44
+ def delete_files_tags(files)
45
+ return if files.empty?
46
+ `cat #{tagf} | grep -v -F #{files.map { |f| " -e \" #{f} \""}.join(' ')} > #{tmp_file} 2> /dev/null`
47
+ if $?.exitstatus == 0
48
+ `mv -f #{tmp_file} #{tagf}`
49
+ puts "Deleted tags for #{files} from #{tagf}"
50
+ else
51
+ puts "Failed to delete tags for #{files} from #{tagf}"
52
+ end
53
+ end
54
+
55
+ private
56
+
57
+ def generate_tags?(file)
58
+ copacetic_with_git?(file) && indexable?(file)
59
+ end
60
+
61
+ def indexable?(file)
62
+ file != tagf && !is_file_excluded(file) && File.readable?(file)
63
+ end
64
+
65
+ def copacetic_with_git?(file)
66
+ !(check_git? && !Helper.is_file_tracked?(file))
67
+ end
68
+
69
+ def check_git?
70
+ @config.respect_git? && Helper.is_git_dir?
71
+ end
72
+
73
+ def is_file_excluded(fname)
74
+ @config.is_ft_excluded(fname.partition('.').last) || @config.is_path_excluded(fname)
75
+ end
76
+
77
+ def assert_executables
78
+ %w(cat grep mv).each do |exe|
79
+ if !Helper.is_executable?(exe)
80
+ raise ExecutableNonExist, "tagrity depends on the executable #{exe}"
81
+ end
82
+ end
83
+ end
84
+
85
+ def tagf
86
+ @config.tagf
87
+ end
88
+
89
+ def tmp_file
90
+ tmpdir = "#{Dir.tmpdir}/tagrity"
91
+ FileUtils.mkdir_p(tmpdir)
92
+ "#{tmpdir}/#{Process.pid}.tmptags"
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,3 @@
1
+ module Tagrity
2
+ VERSION = "0.1.4"
3
+ end
@@ -0,0 +1,36 @@
1
+ lib = File.expand_path("lib", __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "tagrity/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "tagrity"
7
+ spec.version = Tagrity::VERSION
8
+ spec.authors = ["Adam P. Regasz-Rethy"]
9
+ spec.email = ["rethy.spud@gmail.com"]
10
+
11
+ spec.summary = %q{Regenerate tags on file changes.}
12
+ spec.description = %q{Ditto}
13
+ spec.homepage = "https://github.com/RRethy/tagrity"
14
+ spec.license = "MIT"
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = "https://github.com/RRethy/tagrity"
18
+ spec.metadata["changelog_uri"] = "https://github.com/RRethy/tagrity/blob/master/CHANGELOG.md"
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
23
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
24
+ end
25
+ spec.bindir = "exe"
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+
29
+ spec.add_dependency 'thor', '~> 0.20'
30
+ spec.add_dependency 'listen', '~> 3.0'
31
+
32
+ spec.add_development_dependency "bundler", "~> 2.0"
33
+ spec.add_development_dependency "rake", "~> 10.0"
34
+ spec.add_development_dependency "rspec", "~> 3.0"
35
+ spec.add_development_dependency "ripper-tags", "~> 0.8.0"
36
+ end
metadata ADDED
@@ -0,0 +1,157 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tagrity
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.4
5
+ platform: ruby
6
+ authors:
7
+ - Adam P. Regasz-Rethy
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-12-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.20'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.20'
27
+ - !ruby/object:Gem::Dependency
28
+ name: listen
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: ripper-tags
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.8.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 0.8.0
97
+ description: Ditto
98
+ email:
99
+ - rethy.spud@gmail.com
100
+ executables:
101
+ - tagrity
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - ".rspec"
107
+ - ".travis.yml"
108
+ - CODE_OF_CONDUCT.md
109
+ - Gemfile
110
+ - Gemfile.lock
111
+ - LICENSE.txt
112
+ - README.md
113
+ - Rakefile
114
+ - bin/console
115
+ - bin/setup
116
+ - exe/tagrity
117
+ - foobar.rb
118
+ - lib/tagrity.rb
119
+ - lib/tagrity/cli.rb
120
+ - lib/tagrity/commands/start.rb
121
+ - lib/tagrity/commands/status.rb
122
+ - lib/tagrity/commands/stop.rb
123
+ - lib/tagrity/config_file.rb
124
+ - lib/tagrity/file_callbacks.rb
125
+ - lib/tagrity/helper.rb
126
+ - lib/tagrity/pid_file.rb
127
+ - lib/tagrity/provider.rb
128
+ - lib/tagrity/tag_generator.rb
129
+ - lib/tagrity/version.rb
130
+ - tagrity.gemspec
131
+ homepage: https://github.com/RRethy/tagrity
132
+ licenses:
133
+ - MIT
134
+ metadata:
135
+ homepage_uri: https://github.com/RRethy/tagrity
136
+ source_code_uri: https://github.com/RRethy/tagrity
137
+ changelog_uri: https://github.com/RRethy/tagrity/blob/master/CHANGELOG.md
138
+ post_install_message:
139
+ rdoc_options: []
140
+ require_paths:
141
+ - lib
142
+ required_ruby_version: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - ">="
145
+ - !ruby/object:Gem::Version
146
+ version: '0'
147
+ required_rubygems_version: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - ">="
150
+ - !ruby/object:Gem::Version
151
+ version: '0'
152
+ requirements: []
153
+ rubygems_version: 3.0.3
154
+ signing_key:
155
+ specification_version: 4
156
+ summary: Regenerate tags on file changes.
157
+ test_files: []