try-cli 1.9.2 → 1.9.3

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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/VERSION +1 -1
  3. data/try.rb +135 -27
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 00a24afe865c51ab7042ceb3800d665c9446524c48aca062463fad9aeb54a894
4
- data.tar.gz: 3433f233d6191fdd578d37e09ac2f229ee7fe030a13c0ec5ab4a1bce60e5821e
3
+ metadata.gz: 07dc2d2cbbb24c4c934bffceead77e583ef973e1c7f7a08361863c340a373783
4
+ data.tar.gz: 8c110d86811dbecdd9b7a0778605f76d3564f6f9eec697a05da809a98f39b5dd
5
5
  SHA512:
6
- metadata.gz: 89ee4cecc2f8f7d05ba6ac56037db149b43b52efb111615753d975fea0a835e04adb9905c0864ccc3242a3f080510365d1a25b89fb077c04aa958a767a9b91b5
7
- data.tar.gz: 1adc18aa91717d0984dfc468c7844ffe95357d50a1314bea7c2143b38666f7d7531cd933a3d8533e2b22a4f90359ee367580852c08229dfbb99a9dea629148ba
6
+ metadata.gz: c93e4020653e00136115a24cd831e409e00b3e1e9ae48013794b794115f37eae3df599af93c01bd4e571ca45933188c460a4e808e4ad6a78eef5b2384c814ff6
7
+ data.tar.gz: 5487462b36084f6f4b5b7ebe553724d9ac1bddd42246a5d9bea5a53f2702a1be311cfbae448686c68fe73a8134ab73af1bda5d2403ed0d19f5bc62589f33b09c
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.9.2
1
+ 1.9.3
data/try.rb CHANGED
@@ -955,7 +955,7 @@ end
955
955
  # Main execution with OptionParser subcommands
956
956
  if __FILE__ == $0
957
957
 
958
- VERSION = "1.9.2"
958
+ VERSION = "1.9.3"
959
959
 
960
960
  def print_global_help
961
961
  text = <<~HELP
@@ -1176,37 +1176,142 @@ if __FILE__ == $0
1176
1176
  File.expand_path(args.shift)
1177
1177
  end
1178
1178
 
1179
- # Priority: explicit init argument > $TRY_PATH (runtime) > default
1180
1179
  default_path = tries_path || File.expand_path("~/src/tries")
1181
- path_arg = explicit_path ? " --path '#{explicit_path}'" : " --path \"${TRY_PATH:-#{default_path}}\""
1182
- bash_or_zsh_script = <<~SHELL
1183
- try() {
1184
- local out
1185
- out=$(/usr/bin/env ruby '#{script_path}' exec#{path_arg} "$@" 2>/dev/tty)
1186
- if [ $? -eq 0 ]; then
1187
- eval "$out"
1188
- else
1189
- echo "$out"
1190
- fi
1191
- }
1192
- SHELL
1193
-
1194
- fish_path_arg = explicit_path ? " --path '#{explicit_path}'" : " --path (if set -q TRY_PATH; echo \"$TRY_PATH\"; else; echo '#{default_path}'; end)"
1195
- fish_script = <<~SHELL
1196
- function try
1197
- set -l out (/usr/bin/env ruby '#{script_path}' exec#{fish_path_arg} $argv 2>/dev/tty | string collect)
1198
- if test $pipestatus[1] -eq 0
1199
- eval $out
1200
- else
1201
- echo $out
1202
- end
1203
- end
1204
- SHELL
1180
+ shell = fish? ? 'fish' : 'bash'
1181
+ puts init_snippet(shell, script_path, explicit_path, default_path)
1182
+ exit 0
1183
+ end
1184
+
1185
+ def cmd_install!(args, tries_path)
1186
+ script_path = File.expand_path($0)
1187
+
1188
+ explicit_path = if args[0] && args[0].start_with?('/')
1189
+ File.expand_path(args.shift)
1190
+ end
1191
+
1192
+ default_path = tries_path || File.expand_path("~/src/tries")
1193
+
1194
+ shell = detect_shell
1195
+ rc_file = shell_rc_file(shell)
1196
+ snippet = init_snippet(shell, script_path, explicit_path, default_path)
1197
+
1198
+ unless rc_file
1199
+ STDERR.puts "Error: could not determine shell config file"
1200
+ STDERR.puts "Your shell was detected as: #{shell || 'unknown'}"
1201
+ STDERR.puts "Run 'try init' and manually add the output to your shell config."
1202
+ exit 1
1203
+ end
1204
+
1205
+ rc_path = File.expand_path(rc_file)
1206
+
1207
+ # Check if already installed
1208
+ if File.exist?(rc_path) && File.read(rc_path).include?("# try shell integration")
1209
+ STDERR.puts "try is already installed in #{rc_path}"
1210
+ STDERR.puts "To reinstall, remove the '# try shell integration' block first."
1211
+ exit 0
1212
+ end
1213
+
1214
+ block = "\n# try shell integration\n#{snippet}"
1205
1215
 
1206
- puts fish? ? fish_script : bash_or_zsh_script
1216
+ if File.exist?(rc_path) && !File.writable?(rc_path)
1217
+ STDERR.puts "Warning: #{rc_path} is read-only, skipping."
1218
+ STDERR.puts "Run 'try init' and manually add the output to your shell config."
1219
+ exit 1
1220
+ end
1221
+
1222
+ FileUtils.mkdir_p(File.dirname(rc_path))
1223
+ File.open(rc_path, 'a') { |f| f.write(block) }
1224
+ STDERR.puts "Added try shell integration to #{rc_path}"
1225
+ STDERR.puts "Restart your shell or run: source #{rc_path}" unless shell == 'pwsh'
1226
+ STDERR.puts "Restart your shell or run: . $PROFILE" if shell == 'pwsh'
1207
1227
  exit 0
1208
1228
  end
1209
1229
 
1230
+ def detect_shell
1231
+ # Check SHELL env (Unix), then parent process, then PSModulePath for PowerShell
1232
+ shell_env = ENV["SHELL"].to_s
1233
+ return 'fish' if shell_env.include?('fish')
1234
+ return 'zsh' if shell_env.include?('zsh')
1235
+ return 'bash' if shell_env.include?('bash')
1236
+
1237
+ # PowerShell detection: PSModulePath is set in pwsh sessions
1238
+ return 'pwsh' if ENV["PSModulePath"] && !ENV["PSModulePath"].empty?
1239
+
1240
+ # Fallback: check parent process name
1241
+ parent = (`ps c -p #{Process.ppid} -o 'ucomm='`.strip rescue nil)
1242
+ return 'fish' if parent&.include?('fish')
1243
+ return 'zsh' if parent&.include?('zsh')
1244
+ return 'bash' if parent&.include?('bash')
1245
+ return 'pwsh' if parent&.match?(/pwsh|powershell/i)
1246
+
1247
+ nil
1248
+ end
1249
+
1250
+ def shell_rc_file(shell)
1251
+ case shell
1252
+ when 'fish' then '~/.config/fish/config.fish'
1253
+ when 'zsh' then '~/.zshrc'
1254
+ when 'bash'
1255
+ # Prefer .bashrc, fall back to .bash_profile on macOS
1256
+ File.exist?(File.expand_path('~/.bashrc')) ? '~/.bashrc' : '~/.bash_profile'
1257
+ when 'pwsh'
1258
+ # PowerShell profile path from $PROFILE, or the standard location
1259
+ ENV["PROFILE"] || (Gem.win_platform? ?
1260
+ File.join(ENV["USERPROFILE"] || Dir.home, "Documents", "PowerShell", "Microsoft.PowerShell_profile.ps1") :
1261
+ File.join(Dir.home, ".config", "powershell", "Microsoft.PowerShell_profile.ps1"))
1262
+ end
1263
+ end
1264
+
1265
+ def init_snippet(shell, script_path, explicit_path, default_path)
1266
+ case shell
1267
+ when 'fish'
1268
+ fish_path_arg = explicit_path ? " --path '#{explicit_path}'" : " --path (if set -q TRY_PATH; echo \"$TRY_PATH\"; else; echo '#{default_path}'; end)"
1269
+ <<~FISH
1270
+ function try
1271
+ set -l out (/usr/bin/env ruby '#{script_path}' exec#{fish_path_arg} $argv 2>/dev/tty | string collect)
1272
+ if test $pipestatus[1] -eq 0
1273
+ eval $out
1274
+ else
1275
+ echo $out
1276
+ end
1277
+ end
1278
+ FISH
1279
+ when 'pwsh'
1280
+ ps_path_expr = if explicit_path
1281
+ "'#{explicit_path}'"
1282
+ else
1283
+ "$(if ($env:TRY_PATH) { $env:TRY_PATH } else { '#{default_path}' })"
1284
+ end
1285
+ <<~PWSH
1286
+ function try {
1287
+ $tryPath = #{ps_path_expr}
1288
+ $tempErr = [System.IO.Path]::GetTempFileName()
1289
+ $out = & ruby '#{script_path}' exec --path $tryPath @args 2>$tempErr
1290
+ if ($LASTEXITCODE -eq 0) {
1291
+ $out | Invoke-Expression
1292
+ } else {
1293
+ Get-Content $tempErr | Write-Host
1294
+ $out | Write-Output
1295
+ }
1296
+ Remove-Item $tempErr -ErrorAction SilentlyContinue
1297
+ }
1298
+ PWSH
1299
+ else # bash, zsh
1300
+ path_arg = explicit_path ? " --path '#{explicit_path}'" : " --path \"${TRY_PATH:-#{default_path}}\""
1301
+ <<~SH
1302
+ try() {
1303
+ local out
1304
+ out=$(/usr/bin/env ruby '#{script_path}' exec#{path_arg} "$@" 2>/dev/tty)
1305
+ if [ $? -eq 0 ]; then
1306
+ eval "$out"
1307
+ else
1308
+ echo "$out"
1309
+ fi
1310
+ }
1311
+ SH
1312
+ end
1313
+ end
1314
+
1210
1315
  def cmd_cd!(args, tries_path, and_type, and_exit, and_keys, and_confirm)
1211
1316
  if args.first == "clone"
1212
1317
  return cmd_clone!(args[1..-1] || [], tries_path)
@@ -1427,6 +1532,9 @@ if __FILE__ == $0
1427
1532
  when 'init'
1428
1533
  cmd_init!(ARGV, tries_path)
1429
1534
  exit 0
1535
+ when 'install'
1536
+ cmd_install!(ARGV, tries_path)
1537
+ exit 0
1430
1538
  when 'exec'
1431
1539
  sub = ARGV.first
1432
1540
  case sub
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: try-cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.9.2
4
+ version: 1.9.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tobi Lutke