bashisms 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 8ce103809b00b1eadaa9abe09774a096b5ed73afdb4bddbe348708dc66452ac3
4
+ data.tar.gz: 6784e05d427faa209c6bf986e4fd9e59f11744b0edf1dda12c484e8b519cb702
5
+ SHA512:
6
+ metadata.gz: 79ebedd71a25a369256d683411d289bc1e12a90135736a2b831fc977492f2eeae82d5584f669db88eb250413101a58c8532e76cd83fa99b7bf6eca69d6b8d870
7
+ data.tar.gz: b068f5f2e5913016c94b6ab8b6adca4350bd6447177e386f6973ab0a44957e6d02c5ed3004a43770de95e7098a861486c4c43628b7b4bd31f4ddf945844016c0
@@ -0,0 +1,10 @@
1
+ # Code of Conduct
2
+
3
+ "bashisms" follows [The Ruby Community Conduct Guideline](https://www.ruby-lang.org/en/conduct) in all "collaborative space", which is defined as community communications channels (such as mailing lists, submitted patches, commit comments, etc.):
4
+
5
+ * Participants will be tolerant of opposing views.
6
+ * Participants must ensure that their language and actions are free of personal attacks and disparaging personal remarks.
7
+ * When interpreting the words and actions of others, participants should always assume good intentions.
8
+ * Behaviour which can be reasonably considered harassment will not be tolerated.
9
+
10
+ If you have any concerns about behaviour within this project, please contact us at ["john@hawthorn.email"](mailto:"john@hawthorn.email").
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2026 John Hawthorn
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,81 @@
1
+ # Bashisms
2
+
3
+ Bash-style global variables and builtins for Ruby.
4
+
5
+ ```ruby
6
+ gem "bashisms"
7
+ ```
8
+
9
+ ## safe_sleep
10
+
11
+ Finally we're able to implement the venerable [`safe_sleep.sh`](https://github.com/actions/runner/blob/af6c8e6eddef233fdef862c1287d9b013c8aabff/src/Misc/layoutroot/safe_sleep.sh) in Ruby:
12
+
13
+ ```bash
14
+ #!/bin/bash
15
+ SECONDS=0
16
+ while [[ $SECONDS != $1 ]]; do
17
+ :
18
+ done
19
+ ```
20
+
21
+ Now in Ruby, thanks to bashisms:
22
+
23
+ ```ruby
24
+ require "bashisms"
25
+
26
+ def safe_sleep n
27
+ $SECONDS = 0
28
+ while $SECONDS != n
29
+ end
30
+ end
31
+ ```
32
+
33
+ ## Variables
34
+
35
+ ```ruby
36
+ require "bashisms"
37
+
38
+ $RANDOM # => 16838
39
+ $RANDOM # => 4592
40
+
41
+ $SECONDS # => 0
42
+ sleep 2
43
+ $SECONDS # => 2
44
+ $SECONDS = 100
45
+ $SECONDS # => 100
46
+
47
+ $EPOCHSECONDS # => 1739404800
48
+ $EPOCHREALTIME # => 1739404800.123456
49
+
50
+ $LINENO # => 14
51
+
52
+ $HOSTNAME # => "myhost"
53
+ $OSTYPE # => "Linux"
54
+ $MACHTYPE # => "x86_64"
55
+ $PPID # => 1234
56
+ $UID # => 1000
57
+ $EUID # => 1000
58
+
59
+ $BASH_VERSION # => "4.1.2026(4.1.0)-release (ruby)"
60
+
61
+ $IFS = ","
62
+ "a,b,c".split # => ["a", "b", "c"]
63
+
64
+ $COLUMNS # => 120
65
+ $LINES # => 40
66
+ ```
67
+
68
+ ## Builtins
69
+
70
+ ```ruby
71
+ echo "hello world"
72
+
73
+ source "./config.rb"
74
+
75
+ export "FOO=bar"
76
+ ENV["FOO"] # => "bar"
77
+ ```
78
+
79
+ ## License
80
+
81
+ MIT
data/Rakefile ADDED
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "minitest/test_task"
5
+
6
+ Minitest::TestTask.create
7
+
8
+ require "rake/extensiontask"
9
+
10
+ task build: :compile
11
+
12
+ GEMSPEC = Gem::Specification.load("bashisms.gemspec")
13
+
14
+ Rake::ExtensionTask.new("bashisms", GEMSPEC) do |ext|
15
+ ext.lib_dir = "lib/bashisms"
16
+ end
17
+
18
+ task default: %i[clobber compile test]
@@ -0,0 +1,174 @@
1
+ #include "bashisms.h"
2
+ #include <ruby/version.h>
3
+ #include <time.h>
4
+ #include <unistd.h>
5
+ #include <sys/types.h>
6
+ #include <sys/ioctl.h>
7
+ #include <sys/utsname.h>
8
+
9
+ VALUE rb_mBashisms;
10
+
11
+ static struct timespec bashisms_start_time;
12
+
13
+ /* $RANDOM: 0-32767, just like bash */
14
+ static VALUE
15
+ bashisms_random_getter(ID id, VALUE *data)
16
+ {
17
+ return rb_funcall(rb_mKernel, rb_intern("rand"), 1, INT2FIX(32768));
18
+ }
19
+
20
+ static void
21
+ bashisms_random_setter(VALUE val, ID id, VALUE *data)
22
+ {
23
+ rb_funcall(rb_mKernel, rb_intern("srand"), 1, val);
24
+ }
25
+
26
+ /* $SECONDS: seconds since require "bashisms" */
27
+ static VALUE
28
+ bashisms_seconds_getter(ID id, VALUE *data)
29
+ {
30
+ struct timespec now;
31
+ clock_gettime(CLOCK_MONOTONIC, &now);
32
+ long elapsed = now.tv_sec - bashisms_start_time.tv_sec;
33
+ return LONG2FIX(elapsed);
34
+ }
35
+
36
+ static void
37
+ bashisms_seconds_setter(VALUE val, ID id, VALUE *data)
38
+ {
39
+ long offset = NUM2LONG(val);
40
+ struct timespec now;
41
+ clock_gettime(CLOCK_MONOTONIC, &now);
42
+ bashisms_start_time.tv_sec = now.tv_sec - offset;
43
+ bashisms_start_time.tv_nsec = 0;
44
+ }
45
+
46
+ /* $EPOCHSECONDS: Unix timestamp as integer */
47
+ static VALUE
48
+ bashisms_epochseconds_getter(ID id, VALUE *data)
49
+ {
50
+ struct timespec now;
51
+ clock_gettime(CLOCK_REALTIME, &now);
52
+ return LONG2FIX(now.tv_sec);
53
+ }
54
+
55
+ /* $EPOCHREALTIME: Unix timestamp with microseconds as float */
56
+ static VALUE
57
+ bashisms_epochrealtime_getter(ID id, VALUE *data)
58
+ {
59
+ struct timespec now;
60
+ clock_gettime(CLOCK_REALTIME, &now);
61
+ double t = (double)now.tv_sec + (double)now.tv_nsec / 1000000000.0;
62
+ return DBL2NUM(t);
63
+ }
64
+
65
+ /* $HOSTNAME */
66
+ static VALUE
67
+ bashisms_hostname_getter(ID id, VALUE *data)
68
+ {
69
+ char buf[256];
70
+ if (gethostname(buf, sizeof(buf)) == 0) {
71
+ buf[sizeof(buf) - 1] = '\0';
72
+ return rb_str_new_cstr(buf);
73
+ }
74
+ return rb_str_new_cstr("unknown");
75
+ }
76
+
77
+ /* $PPID: parent process ID */
78
+ static VALUE
79
+ bashisms_ppid_getter(ID id, VALUE *data)
80
+ {
81
+ return PIDT2NUM(getppid());
82
+ }
83
+
84
+ /* $UID: real user ID */
85
+ static VALUE
86
+ bashisms_uid_getter(ID id, VALUE *data)
87
+ {
88
+ return UIDT2NUM(getuid());
89
+ }
90
+
91
+ /* $EUID: effective user ID */
92
+ static VALUE
93
+ bashisms_euid_getter(ID id, VALUE *data)
94
+ {
95
+ return UIDT2NUM(geteuid());
96
+ }
97
+
98
+ static VALUE
99
+ bashisms_bash_version_getter(ID id, VALUE *data)
100
+ {
101
+ char buf[256];
102
+ snprintf(buf, sizeof(buf), "4.1.2026(%s)-release (ruby)", ruby_version);
103
+ return rb_str_new_cstr(buf);
104
+ }
105
+
106
+ static VALUE
107
+ bashisms_lineno_getter(ID id, VALUE *data)
108
+ {
109
+ return INT2FIX(rb_sourceline());
110
+ }
111
+
112
+ static VALUE
113
+ bashisms_ostype_getter(ID id, VALUE *data)
114
+ {
115
+ struct utsname buf;
116
+ if (uname(&buf) == 0) {
117
+ return rb_str_new_cstr(buf.sysname);
118
+ }
119
+ return rb_str_new_cstr("unknown");
120
+ }
121
+
122
+ static VALUE
123
+ bashisms_machtype_getter(ID id, VALUE *data)
124
+ {
125
+ struct utsname buf;
126
+ if (uname(&buf) == 0) {
127
+ return rb_str_new_cstr(buf.machine);
128
+ }
129
+ return rb_str_new_cstr("unknown");
130
+ }
131
+
132
+ static VALUE
133
+ bashisms_columns_getter(ID id, VALUE *data)
134
+ {
135
+ struct winsize w;
136
+ if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) == 0 && w.ws_col > 0) {
137
+ return INT2FIX(w.ws_col);
138
+ }
139
+ return INT2FIX(80);
140
+ }
141
+
142
+ static VALUE
143
+ bashisms_lines_getter(ID id, VALUE *data)
144
+ {
145
+ struct winsize w;
146
+ if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) == 0 && w.ws_row > 0) {
147
+ return INT2FIX(w.ws_row);
148
+ }
149
+ return INT2FIX(24);
150
+ }
151
+
152
+ RUBY_FUNC_EXPORTED void
153
+ Init_bashisms(void)
154
+ {
155
+ rb_mBashisms = rb_define_module("Bashisms");
156
+
157
+ clock_gettime(CLOCK_MONOTONIC, &bashisms_start_time);
158
+
159
+ rb_define_virtual_variable("$RANDOM", bashisms_random_getter, bashisms_random_setter);
160
+ rb_define_virtual_variable("$SECONDS", bashisms_seconds_getter, bashisms_seconds_setter);
161
+ rb_define_virtual_variable("$EPOCHSECONDS", bashisms_epochseconds_getter, NULL);
162
+ rb_define_virtual_variable("$EPOCHREALTIME", bashisms_epochrealtime_getter, NULL);
163
+ rb_define_virtual_variable("$HOSTNAME", bashisms_hostname_getter, NULL);
164
+ rb_define_virtual_variable("$PPID", bashisms_ppid_getter, NULL);
165
+ rb_define_virtual_variable("$UID", bashisms_uid_getter, NULL);
166
+ rb_define_virtual_variable("$EUID", bashisms_euid_getter, NULL);
167
+ rb_define_virtual_variable("$BASH_VERSION", bashisms_bash_version_getter, NULL);
168
+ rb_define_virtual_variable("$LINENO", bashisms_lineno_getter, NULL);
169
+ rb_alias_variable(rb_intern("$IFS"), rb_intern("$;"));
170
+ rb_define_virtual_variable("$OSTYPE", bashisms_ostype_getter, NULL);
171
+ rb_define_virtual_variable("$MACHTYPE", bashisms_machtype_getter, NULL);
172
+ rb_define_virtual_variable("$COLUMNS", bashisms_columns_getter, NULL);
173
+ rb_define_virtual_variable("$LINES", bashisms_lines_getter, NULL);
174
+ }
@@ -0,0 +1,6 @@
1
+ #ifndef BASHISMS_H
2
+ #define BASHISMS_H 1
3
+
4
+ #include "ruby.h"
5
+
6
+ #endif /* BASHISMS_H */
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "mkmf"
4
+
5
+ # Makes all symbols private by default to avoid unintended conflict
6
+ # with other gems. To explicitly export symbols you can use RUBY_FUNC_EXPORTED
7
+ # selectively, or entirely remove this flag.
8
+ append_cflags("-fvisibility=hidden")
9
+
10
+ create_makefile("bashisms/bashisms")
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bashisms
4
+ VERSION = "0.1.0"
5
+ end
data/lib/bashisms.rb ADDED
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "bashisms/version"
4
+ require "bashisms/bashisms"
5
+
6
+ module Bashisms
7
+ class Error < StandardError; end
8
+ end
9
+
10
+ module Kernel
11
+ def echo(*args)
12
+ puts(*args)
13
+ end
14
+
15
+ def source(file)
16
+ load(file)
17
+ end
18
+
19
+ def export(pair)
20
+ key, value = pair.to_s.split("=", 2)
21
+ ENV[key] = value
22
+ end
23
+
24
+ private :echo, :source, :export
25
+ end
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bashisms
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - John Hawthorn
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: Bash-style global variables and builtins for Ruby.
13
+ email:
14
+ - john@hawthorn.email
15
+ executables: []
16
+ extensions:
17
+ - ext/bashisms/extconf.rb
18
+ extra_rdoc_files: []
19
+ files:
20
+ - CODE_OF_CONDUCT.md
21
+ - LICENSE.txt
22
+ - README.md
23
+ - Rakefile
24
+ - ext/bashisms/bashisms.c
25
+ - ext/bashisms/bashisms.h
26
+ - ext/bashisms/extconf.rb
27
+ - lib/bashisms.rb
28
+ - lib/bashisms/version.rb
29
+ homepage: https://github.com/jhawthorn/bashisms
30
+ licenses:
31
+ - MIT
32
+ metadata:
33
+ allowed_push_host: https://rubygems.org
34
+ homepage_uri: https://github.com/jhawthorn/bashisms
35
+ source_code_uri: https://github.com/jhawthorn/bashisms
36
+ rdoc_options: []
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 3.2.0
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubygems_version: 4.0.3
51
+ specification_version: 4
52
+ summary: Bashisms for Ruby
53
+ test_files: []