zwerg 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CODE_OF_CONDUCT.md +132 -0
- data/LICENSE.txt +21 -0
- data/README.md +112 -0
- data/Rakefile +8 -0
- data/example_zwerg.yml +52 -0
- data/exe/zwerg +64 -0
- data/lib/zwerg/action_executor.rb +52 -0
- data/lib/zwerg/config.rb +39 -0
- data/lib/zwerg/version.rb +5 -0
- data/lib/zwerg/watcher.rb +84 -0
- data/lib/zwerg.rb +21 -0
- metadata +81 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 8cc6b79e2842c2d7fd73d23c994432be87e4a97f3a1e23c3596fe4658a1f0a6b
|
4
|
+
data.tar.gz: c3f81dcdba96f0255be18f863c28f2cbfe08eecee52d06b7917308b682f6e031
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: bad591c47d47f91a80feb8c0fb3284165d62790dd2ac1e6c0577db42f7dfecd07a92e366897a8d7a4d1734bb984e644ec22fb6838c37107f5b0ecc406afdcc82
|
7
|
+
data.tar.gz: a4be1ecd56849f66b15cc99911258e498a450714cdfa310e3be33b7ddb8a51dfcd6804ae92191937e95f8a8ca0247421e42d8a71574b8578578a9e6bac734b5d
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our
|
6
|
+
community a harassment-free experience for everyone, regardless of age, body
|
7
|
+
size, visible or invisible disability, ethnicity, sex characteristics, gender
|
8
|
+
identity and expression, level of experience, education, socio-economic status,
|
9
|
+
nationality, personal appearance, race, caste, color, religion, or sexual
|
10
|
+
identity and orientation.
|
11
|
+
|
12
|
+
We pledge to act and interact in ways that contribute to an open, welcoming,
|
13
|
+
diverse, inclusive, and healthy community.
|
14
|
+
|
15
|
+
## Our Standards
|
16
|
+
|
17
|
+
Examples of behavior that contributes to a positive environment for our
|
18
|
+
community include:
|
19
|
+
|
20
|
+
* Demonstrating empathy and kindness toward other people
|
21
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
22
|
+
* Giving and gracefully accepting constructive feedback
|
23
|
+
* Accepting responsibility and apologizing to those affected by our mistakes,
|
24
|
+
and learning from the experience
|
25
|
+
* Focusing on what is best not just for us as individuals, but for the overall
|
26
|
+
community
|
27
|
+
|
28
|
+
Examples of unacceptable behavior include:
|
29
|
+
|
30
|
+
* The use of sexualized language or imagery, and sexual attention or advances of
|
31
|
+
any kind
|
32
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
33
|
+
* Public or private harassment
|
34
|
+
* Publishing others' private information, such as a physical or email address,
|
35
|
+
without their explicit permission
|
36
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
37
|
+
professional setting
|
38
|
+
|
39
|
+
## Enforcement Responsibilities
|
40
|
+
|
41
|
+
Community leaders are responsible for clarifying and enforcing our standards of
|
42
|
+
acceptable behavior and will take appropriate and fair corrective action in
|
43
|
+
response to any behavior that they deem inappropriate, threatening, offensive,
|
44
|
+
or harmful.
|
45
|
+
|
46
|
+
Community leaders have the right and responsibility to remove, edit, or reject
|
47
|
+
comments, commits, code, wiki edits, issues, and other contributions that are
|
48
|
+
not aligned to this Code of Conduct, and will communicate reasons for moderation
|
49
|
+
decisions when appropriate.
|
50
|
+
|
51
|
+
## Scope
|
52
|
+
|
53
|
+
This Code of Conduct applies within all community spaces, and also applies when
|
54
|
+
an individual is officially representing the community in public spaces.
|
55
|
+
Examples of representing our community include using an official email address,
|
56
|
+
posting via an official social media account, or acting as an appointed
|
57
|
+
representative at an online or offline event.
|
58
|
+
|
59
|
+
## Enforcement
|
60
|
+
|
61
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
62
|
+
reported to the community leaders responsible for enforcement at
|
63
|
+
[INSERT CONTACT METHOD].
|
64
|
+
All complaints will be reviewed and investigated promptly and fairly.
|
65
|
+
|
66
|
+
All community leaders are obligated to respect the privacy and security of the
|
67
|
+
reporter of any incident.
|
68
|
+
|
69
|
+
## Enforcement Guidelines
|
70
|
+
|
71
|
+
Community leaders will follow these Community Impact Guidelines in determining
|
72
|
+
the consequences for any action they deem in violation of this Code of Conduct:
|
73
|
+
|
74
|
+
### 1. Correction
|
75
|
+
|
76
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed
|
77
|
+
unprofessional or unwelcome in the community.
|
78
|
+
|
79
|
+
**Consequence**: A private, written warning from community leaders, providing
|
80
|
+
clarity around the nature of the violation and an explanation of why the
|
81
|
+
behavior was inappropriate. A public apology may be requested.
|
82
|
+
|
83
|
+
### 2. Warning
|
84
|
+
|
85
|
+
**Community Impact**: A violation through a single incident or series of
|
86
|
+
actions.
|
87
|
+
|
88
|
+
**Consequence**: A warning with consequences for continued behavior. No
|
89
|
+
interaction with the people involved, including unsolicited interaction with
|
90
|
+
those enforcing the Code of Conduct, for a specified period of time. This
|
91
|
+
includes avoiding interactions in community spaces as well as external channels
|
92
|
+
like social media. Violating these terms may lead to a temporary or permanent
|
93
|
+
ban.
|
94
|
+
|
95
|
+
### 3. Temporary Ban
|
96
|
+
|
97
|
+
**Community Impact**: A serious violation of community standards, including
|
98
|
+
sustained inappropriate behavior.
|
99
|
+
|
100
|
+
**Consequence**: A temporary ban from any sort of interaction or public
|
101
|
+
communication with the community for a specified period of time. No public or
|
102
|
+
private interaction with the people involved, including unsolicited interaction
|
103
|
+
with those enforcing the Code of Conduct, is allowed during this period.
|
104
|
+
Violating these terms may lead to a permanent ban.
|
105
|
+
|
106
|
+
### 4. Permanent Ban
|
107
|
+
|
108
|
+
**Community Impact**: Demonstrating a pattern of violation of community
|
109
|
+
standards, including sustained inappropriate behavior, harassment of an
|
110
|
+
individual, or aggression toward or disparagement of classes of individuals.
|
111
|
+
|
112
|
+
**Consequence**: A permanent ban from any sort of public interaction within the
|
113
|
+
community.
|
114
|
+
|
115
|
+
## Attribution
|
116
|
+
|
117
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
118
|
+
version 2.1, available at
|
119
|
+
[https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
|
120
|
+
|
121
|
+
Community Impact Guidelines were inspired by
|
122
|
+
[Mozilla's code of conduct enforcement ladder][Mozilla CoC].
|
123
|
+
|
124
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
125
|
+
[https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
|
126
|
+
[https://www.contributor-covenant.org/translations][translations].
|
127
|
+
|
128
|
+
[homepage]: https://www.contributor-covenant.org
|
129
|
+
[v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
|
130
|
+
[Mozilla CoC]: https://github.com/mozilla/diversity
|
131
|
+
[FAQ]: https://www.contributor-covenant.org/faq
|
132
|
+
[translations]: https://www.contributor-covenant.org/translations
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2025 Yuji Yaginuma
|
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,112 @@
|
|
1
|
+
# Zwerg
|
2
|
+
|
3
|
+
Zwerg is a Ruby gem that watches for file changes and executes configurable actions based on YAML configuration files. It uses the [watchcat](https://github.com/y-yagi/watchcat) gem for efficient file system monitoring.
|
4
|
+
|
5
|
+
## Features
|
6
|
+
|
7
|
+
- Monitor files and directories for changes
|
8
|
+
- Support for recursive directory watching
|
9
|
+
- File pattern matching (glob patterns)
|
10
|
+
- Configurable actions triggered by file changes
|
11
|
+
- Variable substitution in action commands
|
12
|
+
- Command execution triggered by file changes, file operations, logging, file operations, logging
|
13
|
+
- Cross-platform support (Linux, macOS, Windows)
|
14
|
+
|
15
|
+
## Installation
|
16
|
+
|
17
|
+
Add this line to your application's Gemfile:
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
gem 'zwerg'
|
21
|
+
```
|
22
|
+
|
23
|
+
And then execute:
|
24
|
+
|
25
|
+
```bash
|
26
|
+
bundle install
|
27
|
+
```
|
28
|
+
|
29
|
+
Or install it yourself as:
|
30
|
+
|
31
|
+
```bash
|
32
|
+
gem install zwerg
|
33
|
+
```
|
34
|
+
|
35
|
+
## Usage
|
36
|
+
|
37
|
+
1. Create a configuration file (default: `zwerg.yml`):
|
38
|
+
|
39
|
+
```yaml
|
40
|
+
watches:
|
41
|
+
- path: "./src"
|
42
|
+
recursive: true
|
43
|
+
patterns:
|
44
|
+
- "*.rb"
|
45
|
+
- "*.yml"
|
46
|
+
actions:
|
47
|
+
- command: "echo 'Ruby file changed: {{file_name}}'"
|
48
|
+
- command: "rubocop {{file_path}}"
|
49
|
+
```
|
50
|
+
|
51
|
+
2. Run Zwerg:
|
52
|
+
|
53
|
+
```bash
|
54
|
+
zwerg # Uses zwerg.yml
|
55
|
+
zwerg my_config.yml # Uses custom config file
|
56
|
+
```
|
57
|
+
|
58
|
+
## Configuration
|
59
|
+
|
60
|
+
### Watch Configuration
|
61
|
+
|
62
|
+
Each watch entry can have the following properties:
|
63
|
+
|
64
|
+
- `path`: The file or directory to watch (required)
|
65
|
+
- `recursive`: Whether to watch subdirectories (default: true)
|
66
|
+
- `patterns`: Array of glob patterns to match files (optional, matches all if empty)
|
67
|
+
- `actions`: Array of actions to execute when files change (required)
|
68
|
+
|
69
|
+
### Action Configuration
|
70
|
+
|
71
|
+
Each action is simply a shell command:
|
72
|
+
```yaml
|
73
|
+
- command: "echo 'File changed: {{file_path}}'"
|
74
|
+
```
|
75
|
+
|
76
|
+
You can use any shell command, including complex operations with pipes, redirection, and conditional logic:
|
77
|
+
```yaml
|
78
|
+
- command: "if [ -f {{file_path}} ]; then echo 'File exists: {{file_name}}' >> changes.log; fi"
|
79
|
+
```
|
80
|
+
|
81
|
+
### Variable Substitution
|
82
|
+
|
83
|
+
The following variables can be used in action configurations:
|
84
|
+
|
85
|
+
- `{{file_path}}`: Full path to the changed file
|
86
|
+
- `{{file_dir}}`: Directory containing the file
|
87
|
+
- `{{file_name}}`: Name of the file (including extension)
|
88
|
+
- `{{file_base}}`: Name of the file without extension
|
89
|
+
- `{{file_ext}}`: File extension (including the dot)
|
90
|
+
- `{{event_type}}`: Type of event (create, modify, remove, access)
|
91
|
+
|
92
|
+
## Example Configuration
|
93
|
+
|
94
|
+
See `example_zwerg.yml` for a comprehensive example configuration file.
|
95
|
+
|
96
|
+
## Development
|
97
|
+
|
98
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
99
|
+
|
100
|
+
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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
101
|
+
|
102
|
+
## Contributing
|
103
|
+
|
104
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/y-yagi/zwerg. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/y-yagi/zwerg/blob/main/CODE_OF_CONDUCT.md).
|
105
|
+
|
106
|
+
## License
|
107
|
+
|
108
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
109
|
+
|
110
|
+
## Code of Conduct
|
111
|
+
|
112
|
+
Everyone interacting in the Zwerg project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/y-yagi/zwerg/blob/main/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/example_zwerg.yml
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
# Zwerg Configuration File
|
2
|
+
# This file defines which directories to watch and what actions to take when files change
|
3
|
+
|
4
|
+
watches:
|
5
|
+
# Watch a source directory for Ruby files
|
6
|
+
- path: "./src"
|
7
|
+
recursive: true
|
8
|
+
patterns:
|
9
|
+
- "*.rb"
|
10
|
+
- "*.yml"
|
11
|
+
actions:
|
12
|
+
- command: "echo 'Ruby/YAML file changed: {{file_name}}' >> ./logs/changes.log"
|
13
|
+
- command: "rubocop {{file_path}}"
|
14
|
+
|
15
|
+
# Watch a documentation directory
|
16
|
+
- path: "./docs"
|
17
|
+
recursive: true
|
18
|
+
patterns:
|
19
|
+
- "*.md"
|
20
|
+
- "*.txt"
|
21
|
+
actions:
|
22
|
+
- command: "echo 'Documentation updated: {{file_path}}'"
|
23
|
+
- command: "cp {{file_path}} ./backup/docs/"
|
24
|
+
|
25
|
+
# Watch configuration files in the root directory
|
26
|
+
- path: "."
|
27
|
+
recursive: false
|
28
|
+
patterns:
|
29
|
+
- "*.yml"
|
30
|
+
- "*.yaml"
|
31
|
+
- "*.json"
|
32
|
+
actions:
|
33
|
+
- command: "echo '[{{event_type}}] Config file changed: {{file_name}}'"
|
34
|
+
- command: "echo 'Config changed: {{file_path}}' | mail -s 'Config Alert' admin@example.com"
|
35
|
+
|
36
|
+
# Watch for log files and compress them when they get large
|
37
|
+
- path: "./logs"
|
38
|
+
recursive: false
|
39
|
+
patterns:
|
40
|
+
- "*.log"
|
41
|
+
actions:
|
42
|
+
- command: "if [ $(stat -c%s {{file_path}}) -gt 1048576 ]; then gzip {{file_path}}; fi"
|
43
|
+
|
44
|
+
# Each action is a shell command that will be executed when files change
|
45
|
+
|
46
|
+
# Available variables for substitution:
|
47
|
+
# - {{file_path}}: Full path to the changed file
|
48
|
+
# - {{file_dir}}: Directory containing the file
|
49
|
+
# - {{file_name}}: Name of the file (including extension)
|
50
|
+
# - {{file_base}}: Name of the file without extension
|
51
|
+
# - {{file_ext}}: File extension (including the dot)
|
52
|
+
# - {{event_type}}: Type of event (create, modify, remove, access)
|
data/exe/zwerg
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "zwerg"
|
4
|
+
|
5
|
+
def show_help
|
6
|
+
puts <<~HELP
|
7
|
+
Zwerg - A configurable file watcher that executes commands based on YAML configuration
|
8
|
+
|
9
|
+
USAGE:
|
10
|
+
zwerg [CONFIG_FILE] [OPTIONS]
|
11
|
+
|
12
|
+
ARGUMENTS:
|
13
|
+
CONFIG_FILE Path to YAML configuration file (default: zwerg.yml)
|
14
|
+
|
15
|
+
OPTIONS:
|
16
|
+
-h, --help Show this help message
|
17
|
+
|
18
|
+
EXAMPLES:
|
19
|
+
zwerg # Use default zwerg.yml config
|
20
|
+
zwerg my_config.yml # Use custom config file
|
21
|
+
zwerg --help # Show this help
|
22
|
+
|
23
|
+
CONFIGURATION:
|
24
|
+
Create a YAML file with the following structure:
|
25
|
+
|
26
|
+
watches:
|
27
|
+
- path: "./src"
|
28
|
+
recursive: true
|
29
|
+
patterns:
|
30
|
+
- "*.rb"
|
31
|
+
- "*.yml"
|
32
|
+
actions:
|
33
|
+
- command: "echo 'File changed: {{file_name}}'"
|
34
|
+
- command: "rubocop {{file_path}}"
|
35
|
+
|
36
|
+
AVAILABLE VARIABLES:
|
37
|
+
{{file_path}} Full path to the changed file
|
38
|
+
{{file_dir}} Directory containing the file
|
39
|
+
{{file_name}} Name of the file (including extension)
|
40
|
+
{{file_base}} Name of the file without extension
|
41
|
+
{{file_ext}} File extension (including the dot)
|
42
|
+
{{event_type}} Type of event (create, modify, remove, access)
|
43
|
+
|
44
|
+
For more information, visit: https://github.com/y-yagi/zwerg
|
45
|
+
HELP
|
46
|
+
end
|
47
|
+
|
48
|
+
# Check for help flag
|
49
|
+
if ARGV.include?("-h") || ARGV.include?("--help")
|
50
|
+
show_help
|
51
|
+
exit 0
|
52
|
+
end
|
53
|
+
|
54
|
+
config_file = ARGV.find { |arg| !arg.start_with?("-") } || "zwerg.yml"
|
55
|
+
|
56
|
+
begin
|
57
|
+
Zwerg.start(config_file)
|
58
|
+
rescue Zwerg::Error => e
|
59
|
+
puts "Error: #{e.message}"
|
60
|
+
exit 1
|
61
|
+
rescue Interrupt
|
62
|
+
puts "\nGoodbye!"
|
63
|
+
exit 0
|
64
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Zwerg
|
4
|
+
class ActionExecutor
|
5
|
+
def initialize(file_path, event)
|
6
|
+
@file_path = file_path
|
7
|
+
@event = event
|
8
|
+
@file_dir = File.dirname(file_path)
|
9
|
+
@file_name = File.basename(file_path)
|
10
|
+
@file_ext = File.extname(file_path)
|
11
|
+
@file_base = File.basename(file_path, @file_ext)
|
12
|
+
end
|
13
|
+
|
14
|
+
def execute(action)
|
15
|
+
execute_command(action)
|
16
|
+
rescue => e
|
17
|
+
puts "Error executing action #{action}: #{e.message}"
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def execute_command(action)
|
23
|
+
command = substitute_variables(action["command"])
|
24
|
+
puts "Executing: #{command}"
|
25
|
+
|
26
|
+
success = system(command)
|
27
|
+
unless success
|
28
|
+
puts "Command failed with exit code: #{$?.exitstatus}"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def substitute_variables(template)
|
33
|
+
return template unless template.is_a?(String)
|
34
|
+
|
35
|
+
template
|
36
|
+
.gsub("{{file_path}}", @file_path)
|
37
|
+
.gsub("{{file_dir}}", @file_dir)
|
38
|
+
.gsub("{{file_name}}", @file_name)
|
39
|
+
.gsub("{{file_base}}", @file_base)
|
40
|
+
.gsub("{{file_ext}}", @file_ext)
|
41
|
+
.gsub("{{event_type}}", determine_event_type)
|
42
|
+
end
|
43
|
+
|
44
|
+
def determine_event_type
|
45
|
+
return "create" if @event.kind.create?
|
46
|
+
return "modify" if @event.kind.modify?
|
47
|
+
return "remove" if @event.kind.remove?
|
48
|
+
return "access" if @event.kind.access?
|
49
|
+
"unknown"
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
data/lib/zwerg/config.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "psych"
|
4
|
+
|
5
|
+
module Zwerg
|
6
|
+
class Config
|
7
|
+
attr_reader :watches
|
8
|
+
|
9
|
+
def initialize(data)
|
10
|
+
@watches = parse_watches(data["watches"] || [])
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.load(file_path)
|
14
|
+
unless File.exist?(file_path)
|
15
|
+
raise Error, "Configuration file not found: #{file_path}"
|
16
|
+
end
|
17
|
+
|
18
|
+
begin
|
19
|
+
data = Psych.load_file(file_path)
|
20
|
+
new(data)
|
21
|
+
rescue Psych::SyntaxError => e
|
22
|
+
raise Error, "Invalid YAML syntax in #{file_path}: #{e.message}"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def parse_watches(watches_data)
|
29
|
+
watches_data.map do |watch_config|
|
30
|
+
{
|
31
|
+
path: watch_config["path"],
|
32
|
+
recursive: watch_config.fetch("recursive", true),
|
33
|
+
patterns: watch_config["patterns"] || [],
|
34
|
+
actions: watch_config["actions"] || []
|
35
|
+
}
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,84 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "watchcat"
|
4
|
+
|
5
|
+
module Zwerg
|
6
|
+
class Watcher
|
7
|
+
def initialize(config)
|
8
|
+
@config = config
|
9
|
+
@watchers = []
|
10
|
+
end
|
11
|
+
|
12
|
+
def start
|
13
|
+
puts "Starting Zwerg file watcher..."
|
14
|
+
|
15
|
+
@config.watches.each do |watch_config|
|
16
|
+
start_watching_path(watch_config)
|
17
|
+
end
|
18
|
+
|
19
|
+
puts "Zwerg is now watching for file changes. Press Ctrl+C to stop."
|
20
|
+
|
21
|
+
# Keep the main thread alive
|
22
|
+
begin
|
23
|
+
sleep
|
24
|
+
rescue Interrupt
|
25
|
+
puts "\nStopping Zwerg..."
|
26
|
+
stop
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def stop
|
31
|
+
@watchers.each(&:stop)
|
32
|
+
@watchers.clear
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def start_watching_path(watch_config)
|
38
|
+
path = watch_config[:path]
|
39
|
+
|
40
|
+
unless File.exist?(path)
|
41
|
+
puts "Warning: Path does not exist: #{path}"
|
42
|
+
return
|
43
|
+
end
|
44
|
+
|
45
|
+
puts "Watching: #{path} (recursive: #{watch_config[:recursive]})"
|
46
|
+
|
47
|
+
watcher = Watchcat.watch(
|
48
|
+
path,
|
49
|
+
recursive: watch_config[:recursive],
|
50
|
+
wait_until_startup: true
|
51
|
+
) do |event|
|
52
|
+
handle_file_event(event, watch_config)
|
53
|
+
end
|
54
|
+
|
55
|
+
@watchers << watcher
|
56
|
+
end
|
57
|
+
|
58
|
+
def handle_file_event(event, watch_config)
|
59
|
+
event.paths.each do |file_path|
|
60
|
+
next unless should_process_file?(file_path, watch_config[:patterns])
|
61
|
+
|
62
|
+
puts "File changed: #{file_path}"
|
63
|
+
execute_actions(file_path, event, watch_config[:actions])
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def should_process_file?(file_path, patterns)
|
68
|
+
return true if patterns.empty?
|
69
|
+
|
70
|
+
patterns.any? do |pattern|
|
71
|
+
File.fnmatch?(pattern, File.basename(file_path)) ||
|
72
|
+
File.fnmatch?(pattern, file_path)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def execute_actions(file_path, event, actions)
|
77
|
+
executor = ActionExecutor.new(file_path, event)
|
78
|
+
|
79
|
+
actions.each do |action|
|
80
|
+
executor.execute(action)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
data/lib/zwerg.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "zwerg/version"
|
4
|
+
require_relative "zwerg/watcher"
|
5
|
+
require_relative "zwerg/config"
|
6
|
+
require_relative "zwerg/action_executor"
|
7
|
+
|
8
|
+
module Zwerg
|
9
|
+
class Error < StandardError; end
|
10
|
+
|
11
|
+
class << self
|
12
|
+
def start(config_file)
|
13
|
+
config_file ||= "zwerg.yml"
|
14
|
+
config = Config.load(config_file)
|
15
|
+
watcher = Watcher.new(config)
|
16
|
+
watcher.start
|
17
|
+
rescue => e
|
18
|
+
raise Error, "Failed to start Zwerg: #{e.message}"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: zwerg
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Yuji Yaginuma
|
8
|
+
bindir: exe
|
9
|
+
cert_chain: []
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
11
|
+
dependencies:
|
12
|
+
- !ruby/object:Gem::Dependency
|
13
|
+
name: watchcat
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
15
|
+
requirements:
|
16
|
+
- - "~>"
|
17
|
+
- !ruby/object:Gem::Version
|
18
|
+
version: 0.3.0
|
19
|
+
type: :runtime
|
20
|
+
prerelease: false
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
22
|
+
requirements:
|
23
|
+
- - "~>"
|
24
|
+
- !ruby/object:Gem::Version
|
25
|
+
version: 0.3.0
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: psych
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - "~>"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '5.0'
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - "~>"
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '5.0'
|
40
|
+
email:
|
41
|
+
- yuuji.yaginuma@gmail.com
|
42
|
+
executables:
|
43
|
+
- zwerg
|
44
|
+
extensions: []
|
45
|
+
extra_rdoc_files: []
|
46
|
+
files:
|
47
|
+
- CODE_OF_CONDUCT.md
|
48
|
+
- LICENSE.txt
|
49
|
+
- README.md
|
50
|
+
- Rakefile
|
51
|
+
- example_zwerg.yml
|
52
|
+
- exe/zwerg
|
53
|
+
- lib/zwerg.rb
|
54
|
+
- lib/zwerg/action_executor.rb
|
55
|
+
- lib/zwerg/config.rb
|
56
|
+
- lib/zwerg/version.rb
|
57
|
+
- lib/zwerg/watcher.rb
|
58
|
+
homepage: https://github.com/y-yagi/zwerg
|
59
|
+
licenses:
|
60
|
+
- MIT
|
61
|
+
metadata:
|
62
|
+
homepage_uri: https://github.com/y-yagi/zwerg
|
63
|
+
source_code_uri: https://github.com/y-yagi/zwerg
|
64
|
+
rdoc_options: []
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: 3.2.0
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
requirements: []
|
78
|
+
rubygems_version: 3.6.7
|
79
|
+
specification_version: 4
|
80
|
+
summary: A configurable file watcher that executes commands based on YAML configuration.
|
81
|
+
test_files: []
|