cruise 0.0.0 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e6c28ff18160ebdea05cff957fa6d2c3933a74776541a241b194b36c4ec382d1
4
- data.tar.gz: 0e963d79323ee6ea95684080d0194fad512009f3429664ffa2c6d32aa3a0d0ee
3
+ metadata.gz: 987534c3a5048aa06fd63879c5788c25e39cee765722c80d3c64da33472d06f8
4
+ data.tar.gz: 32f9ec736fab2350f7d299f7eeb09a78f4826b8d377c1ae12fe8fb19509efbcd
5
5
  SHA512:
6
- metadata.gz: 9d46bd683e1ddff54bce8693b613c2d589f968bc6b0d1d87359d1eb090a5d4f05109abfb9d8a8eb9c11cdaf4d1bfe7abd5fe73801a3b68c07ee6881f1a983e7b
7
- data.tar.gz: f135cf89f25790b30c507949bc2d9dfb27d6dcbe12eeefc710be9165a5601e258abd33771346f66f7d69b28b9d2ec689214488ea46d0d4aec639ee3ed5489e69
6
+ metadata.gz: fce59791b4839f1090c02e02ae88d6874ecce6e8187107ce1c9538cabff63efee8be63992b7de581196b25834894cf76a46d1766cdf4ed826ef50e9caa538c19
7
+ data.tar.gz: 2c5d792a2ac97d23edae8c3ac287b07f8faa7187f5fd13d1fb5d0578d3bdedf00af95591445c3283071e27a8bebc1a44ad30c195276a5f35f9d0410153a9c6c9
data/Cargo.toml ADDED
@@ -0,0 +1,3 @@
1
+ [workspace]
2
+ members = ["ext/cruise"]
3
+ resolver = "2"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2026 Marco Roth
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/Rakefile CHANGED
@@ -1,12 +1,82 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "bundler/gem_tasks"
4
- require "minitest/test_task"
4
+ require "rake/testtask"
5
5
 
6
- Minitest::TestTask.create
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/*_test.rb"]
10
+ end
7
11
 
8
- require "rubocop/rake_task"
12
+ begin
13
+ require "rake/extensiontask"
14
+ require "rb_sys"
15
+ require "rb_sys/extensiontask"
9
16
 
10
- RuboCop::RakeTask.new
17
+ PLATFORMS = [
18
+ "aarch64-linux-gnu",
19
+ "aarch64-linux-musl",
20
+ "arm-linux-gnu",
21
+ "arm64-darwin",
22
+ "x86_64-darwin",
23
+ "x86_64-linux-gnu",
24
+ "x86_64-linux-musl"
25
+ ].freeze
11
26
 
12
- task default: %i[test rubocop]
27
+ RB_SYS_PLATFORM_MAP = {
28
+ "aarch64-linux-gnu" => "aarch64-linux",
29
+ "aarch64-linux-musl" => "aarch64-linux-musl",
30
+ "arm-linux-gnu" => "arm-linux",
31
+ "arm64-darwin" => "arm64-darwin",
32
+ "x86_64-darwin" => "x86_64-darwin",
33
+ "x86_64-linux-gnu" => "x86_64-linux",
34
+ "x86_64-linux-musl" => "x86_64-linux-musl"
35
+ }.freeze
36
+
37
+ RbSys::ExtensionTask.new("cruise", Gem::Specification.load("cruise.gemspec")) do |ext|
38
+ ext.lib_dir = "lib/cruise"
39
+ ext.cross_compile = true
40
+ ext.cross_platform = PLATFORMS
41
+ end
42
+
43
+ namespace "gem" do
44
+ task "prepare" do
45
+ require "rake_compiler_dock"
46
+
47
+ sh "bundle config set cache_all true"
48
+
49
+ gemspec_path = File.expand_path("./cruise.gemspec", __dir__)
50
+ spec = eval(File.read(gemspec_path), binding, gemspec_path)
51
+
52
+ RakeCompilerDock.set_ruby_cc_version(spec.required_ruby_version.as_list)
53
+ rescue LoadError
54
+ abort "rake_compiler_dock is required for this task"
55
+ end
56
+
57
+ PLATFORMS.each do |platform|
58
+ desc "Build all native binary gems in parallel"
59
+ multitask "native" => platform
60
+
61
+ desc "Build the native gem for #{platform}"
62
+ task platform => "prepare" do
63
+ rb_sys_platform = RB_SYS_PLATFORM_MAP.fetch(platform)
64
+
65
+ RakeCompilerDock.sh(
66
+ "bundle install && bundle exec rake native:#{platform} gem RUBY_CC_VERSION='#{ENV.fetch("RUBY_CC_VERSION", nil)}'",
67
+ platform: platform,
68
+ image: "rbsys/#{rb_sys_platform}:#{RbSys::VERSION}"
69
+ )
70
+ end
71
+ end
72
+ end
73
+ rescue LoadError => e
74
+ warn "WARNING: Failed to load extension tasks: #{e.message}"
75
+
76
+ desc "Compile task not available (rake-compiler not installed)"
77
+ task :compile do
78
+ abort "rake-compiler is required: #{e.message}\n\nRun: bundle install"
79
+ end
80
+ end
81
+
82
+ task default: [:compile, :test]
data/cruise.gemspec CHANGED
@@ -13,20 +13,26 @@ Gem::Specification.new do |spec|
13
13
  spec.homepage = "https://github.com/marcoroth/cruise"
14
14
  spec.license = "MIT"
15
15
 
16
- spec.required_ruby_version = ">= 3.1.0"
16
+ spec.required_ruby_version = ">= 3.2.0"
17
17
  spec.require_paths = ["lib"]
18
18
 
19
19
  spec.files = Dir[
20
20
  "cruise.gemspec",
21
21
  "LICENSE.txt",
22
+ "Cargo.toml",
22
23
  "Rakefile",
23
- "lib/**/*.rb"
24
+ "lib/**/*.rb",
25
+ "ext/**/*.{rs,toml,rb,lock}"
24
26
  ]
25
27
 
28
+ spec.extensions = ["ext/cruise/extconf.rb"]
29
+
26
30
  spec.metadata["allowed_push_host"] = "https://rubygems.org"
27
31
  spec.metadata["rubygems_mfa_required"] = "true"
28
32
  spec.metadata["homepage_uri"] = spec.homepage
29
33
  spec.metadata["changelog_uri"] = "#{spec.homepage}/releases"
30
34
  spec.metadata["source_code_uri"] = spec.homepage
31
35
  spec.metadata["bug_tracker_uri"] = "#{spec.homepage}/issues"
36
+
37
+ spec.add_dependency "rb_sys"
32
38
  end
@@ -0,0 +1,16 @@
1
+ [package]
2
+ name = "cruise"
3
+ version = "0.1.0"
4
+ edition = "2024"
5
+ license = "MIT"
6
+ publish = false
7
+
8
+ [dependencies]
9
+ magnus = { version = "0.8", features = ["rb-sys"] }
10
+ rb-sys = { version = "0.9" }
11
+ notify = { version = "7", features = ["macos_fsevent"] }
12
+ notify-debouncer-full = "0.4"
13
+ globset = "0.4"
14
+
15
+ [lib]
16
+ crate-type = ["cdylib"]
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "mkmf"
4
+ require "rb_sys/mkmf"
5
+
6
+ create_rust_makefile("cruise/cruise")
@@ -0,0 +1,213 @@
1
+ use std::path::PathBuf;
2
+
3
+ use globset::{Glob, GlobSet, GlobSetBuilder};
4
+ use magnus::{method, prelude::*, value::Lazy, Error, IntoValue, RClass, RModule, Ruby, Value};
5
+
6
+ use notify::event::{CreateKind, ModifyKind, RemoveKind};
7
+ use notify::{EventKind, RecursiveMode};
8
+ use notify_debouncer_full::{new_debouncer, DebounceEventResult};
9
+
10
+ static MODULE: Lazy<RModule> = Lazy::new(|ruby| ruby.define_module("Cruise").expect("failed to define Cruise module"));
11
+
12
+ static EVENT_CLASS: Lazy<RClass> = Lazy::new(|ruby| {
13
+ let module = ruby.get_inner(&MODULE);
14
+ let class = module.define_class("Event", ruby.class_object()).expect("failed to define Cruise::Event class");
15
+
16
+ class
17
+ .define_method("path", method!(CruiseEvent::path, 0))
18
+ .expect("failed to define path method");
19
+
20
+ class
21
+ .define_method("kind", method!(CruiseEvent::kind, 0))
22
+ .expect("failed to define kind method");
23
+
24
+ class
25
+ .define_method("inspect", method!(CruiseEvent::inspect, 0))
26
+ .expect("failed to define inspect method");
27
+
28
+ class
29
+ .define_method("to_s", method!(CruiseEvent::to_s, 0))
30
+ .expect("failed to define to_s method");
31
+
32
+ class
33
+ });
34
+
35
+ #[magnus::wrap(class = "Cruise::Event", free_immediately)]
36
+ struct CruiseEvent {
37
+ path: String,
38
+ kind: String,
39
+ }
40
+
41
+ impl CruiseEvent {
42
+ fn path(&self) -> &str {
43
+ &self.path
44
+ }
45
+
46
+ fn kind(&self) -> &str {
47
+ &self.kind
48
+ }
49
+
50
+ fn inspect(&self) -> String {
51
+ format!("#<Cruise::Event kind={:?} path={:?}>", self.kind, self.path)
52
+ }
53
+
54
+ fn to_s(&self) -> String {
55
+ format!("{}: {}", self.kind, self.path)
56
+ }
57
+ }
58
+
59
+ fn event_kind_to_string(kind: &EventKind) -> &'static str {
60
+ match kind {
61
+ EventKind::Create(CreateKind::File) => "created",
62
+ EventKind::Create(CreateKind::Folder) => "created",
63
+ EventKind::Create(_) => "created",
64
+ EventKind::Modify(ModifyKind::Data(_)) => "modified",
65
+ EventKind::Modify(ModifyKind::Name(_)) => "renamed",
66
+ EventKind::Modify(_) => "modified",
67
+ EventKind::Remove(RemoveKind::File) => "removed",
68
+ EventKind::Remove(RemoveKind::Folder) => "removed",
69
+ EventKind::Remove(_) => "removed",
70
+ EventKind::Access(_) => "accessed",
71
+ EventKind::Any | EventKind::Other => "changed",
72
+ }
73
+ }
74
+
75
+ fn build_glob_set(patterns: Vec<String>) -> Result<Option<GlobSet>, Error> {
76
+ if patterns.is_empty() {
77
+ return Ok(None);
78
+ }
79
+
80
+ let mut builder = GlobSetBuilder::new();
81
+
82
+ for pattern in &patterns {
83
+ let glob = Glob::new(pattern).map_err(|error| {
84
+ Error::new(
85
+ magnus::Ruby::get().unwrap().exception_arg_error(),
86
+ format!("Invalid glob pattern '{}': {}", pattern, error),
87
+ )
88
+ })?;
89
+ builder.add(glob);
90
+ }
91
+
92
+ let set = builder.build().map_err(|error| {
93
+ Error::new(
94
+ magnus::Ruby::get().unwrap().exception_runtime_error(),
95
+ format!("Failed to build glob set: {}", error),
96
+ )
97
+ })?;
98
+
99
+ Ok(Some(set))
100
+ }
101
+
102
+ enum WaitResult {
103
+ Event(notify::Event),
104
+ Timeout,
105
+ Disconnected,
106
+ }
107
+
108
+ unsafe extern "C" fn wait_for_event(data: *mut std::ffi::c_void) -> *mut std::ffi::c_void {
109
+ let receiver = unsafe { &*(data as *const std::sync::mpsc::Receiver<notify::Event>) };
110
+
111
+ let result = match receiver.recv_timeout(std::time::Duration::from_millis(200)) {
112
+ Ok(event) => Box::new(WaitResult::Event(event)),
113
+ Err(std::sync::mpsc::RecvTimeoutError::Timeout) => Box::new(WaitResult::Timeout),
114
+ Err(std::sync::mpsc::RecvTimeoutError::Disconnected) => Box::new(WaitResult::Disconnected),
115
+ };
116
+
117
+ Box::into_raw(result) as *mut std::ffi::c_void
118
+ }
119
+
120
+ unsafe extern "C" fn unblock_wait(_data: *mut std::ffi::c_void) {}
121
+
122
+ fn watch(
123
+ ruby: &Ruby,
124
+ paths: Vec<String>,
125
+ callback: magnus::block::Proc,
126
+ debounce: f64,
127
+ glob_patterns: Vec<String>,
128
+ only_kinds: Vec<String>,
129
+ ) -> Result<magnus::Value, Error> {
130
+ let debounce_duration = std::time::Duration::from_secs_f64(debounce);
131
+ let glob_set = build_glob_set(glob_patterns)?;
132
+ let filter_kinds = if only_kinds.is_empty() { None } else { Some(only_kinds) };
133
+
134
+ let (sender, receiver) = std::sync::mpsc::channel::<notify::Event>();
135
+
136
+ let mut debouncer = new_debouncer(debounce_duration, None, move |result: DebounceEventResult| {
137
+ if let Ok(events) = result {
138
+ for debounced_event in events {
139
+ let _ = sender.send(debounced_event.event);
140
+ }
141
+ }
142
+ })
143
+ .map_err(|error| Error::new(ruby.exception_runtime_error(), format!("Failed to create watcher: {error}")))?;
144
+
145
+ for path in &paths {
146
+ let watch_path = PathBuf::from(path);
147
+
148
+ if !watch_path.exists() {
149
+ return Err(Error::new(ruby.exception_arg_error(), format!("Path does not exist: {}", watch_path.display())));
150
+ }
151
+
152
+ debouncer
153
+ .watch(&watch_path, RecursiveMode::Recursive)
154
+ .map_err(|error| Error::new(ruby.exception_runtime_error(), format!("Failed to watch path: {error}")))?;
155
+ }
156
+
157
+ loop {
158
+ let result = unsafe {
159
+ rb_sys::rb_thread_call_without_gvl(
160
+ Some(wait_for_event),
161
+ &receiver as *const _ as *mut std::ffi::c_void,
162
+ Some(unblock_wait),
163
+ std::ptr::null_mut(),
164
+ )
165
+ };
166
+
167
+ let wait_result = unsafe { *Box::from_raw(result as *mut WaitResult) };
168
+
169
+ match &wait_result {
170
+ WaitResult::Event(event) => {
171
+ let kind_string = event_kind_to_string(&event.kind);
172
+
173
+ if let Some(ref allowed) = filter_kinds {
174
+ if !allowed.iter().any(|kind| kind == kind_string) {
175
+ continue;
176
+ }
177
+ }
178
+
179
+ for path in &event.paths {
180
+ if let Some(ref globs) = glob_set {
181
+ if !globs.is_match(path) {
182
+ continue;
183
+ }
184
+ }
185
+
186
+ let cruise_event = CruiseEvent {
187
+ path: path.to_string_lossy().to_string(),
188
+ kind: kind_string.to_string(),
189
+ };
190
+
191
+ let value: Value = cruise_event.into_value_with(ruby);
192
+ callback.call::<_, Value>((value,))?;
193
+ }
194
+ }
195
+ WaitResult::Timeout => {
196
+ continue;
197
+ }
198
+ WaitResult::Disconnected => {
199
+ return Err(Error::new(ruby.exception_runtime_error(), "Watcher channel disconnected unexpectedly"));
200
+ }
201
+ }
202
+ }
203
+ }
204
+
205
+ #[magnus::init]
206
+ fn init(ruby: &Ruby) -> Result<(), Error> {
207
+ let module = ruby.get_inner(&MODULE);
208
+ let _ = ruby.get_inner(&EVENT_CLASS);
209
+
210
+ module.define_module_function("_watch", magnus::function!(watch, 5))?;
211
+
212
+ Ok(())
213
+ }
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Cruise
4
- VERSION = "0.0.0"
4
+ VERSION = "0.1.0"
5
5
  end
data/lib/cruise.rb CHANGED
@@ -2,7 +2,33 @@
2
2
 
3
3
  require_relative "cruise/version"
4
4
 
5
+ begin
6
+ ruby_version = RUBY_VERSION.split(".")[0..1].join(".")
7
+
8
+ begin
9
+ require "cruise/#{ruby_version}/cruise"
10
+ rescue LoadError
11
+ require "cruise/cruise"
12
+ end
13
+ rescue LoadError => e
14
+ raise LoadError, "Failed to load Cruise native extension: #{e.message}"
15
+ end
16
+
5
17
  module Cruise
6
- class Error < StandardError; end
7
- # Your code goes here...
18
+ DEFAULT_DEBOUNCE = 0.1
19
+
20
+ class << self
21
+ def watch(*args, glob: nil, debounce: DEFAULT_DEBOUNCE, only: nil, callback: nil, &block)
22
+ callback = block || callback
23
+ paths = args.flatten.grep(String)
24
+
25
+ raise ArgumentError, "Cruise.watch requires at least one path" if paths.empty?
26
+ raise ArgumentError, "Cruise.watch requires a block or callback" unless callback
27
+
28
+ glob_patterns = glob ? Array(glob) : []
29
+ only_kinds = only ? Array(only).map(&:to_s) : []
30
+
31
+ _watch(paths, callback, debounce.to_f, glob_patterns, only_kinds)
32
+ end
33
+ end
8
34
  end
metadata CHANGED
@@ -1,24 +1,44 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cruise
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marco Roth
8
8
  bindir: bin
9
9
  cert_chain: []
10
10
  date: 1980-01-02 00:00:00.000000000 Z
11
- dependencies: []
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: rb_sys
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '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'
12
26
  description: Cruise is a Rust-powered file system watcher with native OS integration.
13
27
  Uses FSEvents on macOS and inotify on Linux.
14
28
  email:
15
29
  - marco.roth@intergga.ch
16
30
  executables: []
17
- extensions: []
31
+ extensions:
32
+ - ext/cruise/extconf.rb
18
33
  extra_rdoc_files: []
19
34
  files:
35
+ - Cargo.toml
36
+ - LICENSE.txt
20
37
  - Rakefile
21
38
  - cruise.gemspec
39
+ - ext/cruise/Cargo.toml
40
+ - ext/cruise/extconf.rb
41
+ - ext/cruise/src/lib.rs
22
42
  - lib/cruise.rb
23
43
  - lib/cruise/version.rb
24
44
  homepage: https://github.com/marcoroth/cruise
@@ -38,7 +58,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
38
58
  requirements:
39
59
  - - ">="
40
60
  - !ruby/object:Gem::Version
41
- version: 3.1.0
61
+ version: 3.2.0
42
62
  required_rubygems_version: !ruby/object:Gem::Requirement
43
63
  requirements:
44
64
  - - ">="