Skip to content

feat: support importing local scripts with content-based language detection - #3

Closed
zrtnb6 wants to merge 1 commit into
RuochenLyu:mainfrom
zrtnb6:feature/import-local-scripts
Closed

zrtnb6 wants to merge 1 commit into
RuochenLyu:mainfrom
zrtnb6:feature/import-local-scripts

Conversation

@zrtnb6

@zrtnb6 zrtnb6 commented Nov 22, 2026

Copy link
Copy Markdown

Summary

Adds a way to import an existing script when creating a task, instead of pasting its contents by hand.

Features

  • Import from a local text file through a file picker with no content-type restriction, so .sh, .py, .rb, .txt, and extension-less files are all selectable.
  • Detect the language from the content, not the suffix:
    • a shebang wins when present, including the #!/usr/bin/env -S node --enable-source-maps form;
    • otherwise the first 4000 characters are sniffed for python / node / ruby / perl / shell features;
    • the extension is only consulted as a fallback, and the import carries a warning when it is.
  • Normalize the text: strip a UTF-8 BOM and convert CRLF / lone CR to LF.
  • Prepend a shebang when the source has none. run.sh is executed directly, so a file without one cannot run.
  • Reject binary input (NUL byte) and files above 512 KiB with a localized error instead of importing garbage.
  • When nothing is recognized, import as zsh with a warning rather than refusing the file. A plain-text note is easy to delete; a script rejected by mistake is not.

Testing

Environment: macOS 26.5, Xcode 26.5, Intel host.

xcodebuild test -project RunShelf.xcodeproj -scheme RunShelf \
  -destination 'platform=macOS' CODE_SIGNING_ALLOWED=NO
→ Executed 112 tests, with 1 test skipped and 1 failure
  • ScriptImporterTests: 14/14 pass. Covers direct and env -S shebang detection, content-over-extension, extension fallback with warning, unknown-text fallback, CRLF/BOM normalization, empty input, binary rejection, oversized rejection, and reading a real file from disk.
  • The one failure is testBackgroundPriorityRunsBelowStandard. It asserts a probe collects two ps -o pri= samples and gets 0 here, because ps is blocked for the process running the tests in this environment (operation not permitted). It is unrelated to this change and also fails on unmodified main here.
  • Static gates from scripts/check.sh that do not need Bundler all pass: JSON schemas parse, generate_cli_schemas.rb --check in sync, sync_xcodeproj_files.rb --check in sync, ruby -c / bash -n on every script, plutil -lint on the LaunchAgent plist, git diff --check clean.
  • scripts/check.sh could not run end to end here: its first step is bundle check, and the Gemfile pins Bundler 2.4.22 while this machine only has system Ruby 2.6. Its Tests and Universal Debug build phases were run directly instead; the Universal build produces an x86_64 arm64 binary.

The repo has no UI test target, so the new card is covered by unit tests at the model layer plus manual verification. I am not claiming automated UI coverage.

Motivation

Reusing an existing script currently means opening it in an editor and copying the whole file into the new-task sheet. That is fine for a five-line one-liner and painful for a 200-line script, and the copied task loses any link to the file it came from. Importing keeps the workflow file-native, which is how the rest of Run Shelf already treats tasks and run history.

Checklist

  • scripts/check.sh — every gate that does not require Bundler passes (see Testing for the two that cannot run here, and why)
  • Commit message follows Conventional Commits 1.0.0
  • New Swift files registered with bundle exec ruby scripts/sync_xcodeproj_files.rb
  • schemas/*.json untouched, so GeneratedSchemas.swift needs no regeneration — nothing to tick here
  • Tests write only to per-case temporary directories and never to the user's Run Shelf data directory

…ection

- Add ScriptImporter: detect the language from content (shebang, then
  syntax sniffing, then extension fallback), normalize BOM and CRLF, and
  prepend a shebang when the source has none
- Add an "Import Script" card to the new-task sheet using NSOpenPanel
  with no content-type restriction
- Show the source file, detected language, and line count after import,
  and surface fallback warnings as amber notes
- Add 14 unit tests covering detection, normalization, and the
  binary/oversized rejection paths
- Document the feature in README.md and README.en.md
RuochenLyu added a commit that referenced this pull request Nov 25, 2026
The new-task sheet gains an eighth card, 导入文件, that copies a local
file into the task's run.sh via the file picker or drag-and-drop.

The interpreter comes only from reliable signals: a byte-0 shebang is
kept verbatim, otherwise the file extension picks one, otherwise zsh.
Content sniffing is deliberately absent; it misreads ordinary shell,
Perl, Ruby and ESM files as Python and fails silently.

Interpreter checks use launchd's fixed PATH, which both scheduled and
manual runs inherit, so `#!/usr/bin/env node` that works in Terminal is
flagged before the task exists. Chosen interpreters resolve Homebrew
first and are written as absolute paths.

Non-regular files, files over 128 KiB, UTF-16, binary, non-UTF-8 and
empty files are rejected with one short message each. No schema, CLI,
lock or on-disk format change.

Supersedes #3.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@RuochenLyu

Copy link
Copy Markdown
Owner

感谢这个 PR,「从本地文件导入脚本」确实是我们缺的功能,方向完全认同。

我们没有直接合并,而是按项目的约束重新实现了一版,已随 Run Shelf 1.7.0 发布:https://github.com/RuochenLyu/run-shelf/releases/tag/v1.7.0

和这个 PR 的主要差别,供参考:

  • 不按内容猜语言。 正则嗅探在普通脚本上会误判:.sh 里的 awk '{print($1)}'、Perl 的 print(、Ruby 的 def f(x)、.mjs 里的 import x from 都会被认成 Python,而且失败是静默的。新实现只看两种可靠信号:第 0 字节的 #!(原样保留,缩进的 #! 不算 shebang),以及扩展名;都没有时补 #!/bin/zsh。凡是补了 shebang,表单都会写明补了哪一行。
  • 按后台运行时的 PATH 检查解释器。 LaunchAgent 和从 Dock 打开的 App 拿到的 PATH 都是 /usr/bin:/bin:/usr/sbin:/sbin,所以 #!/usr/bin/env node 在终端里能跑,在 Run Shelf 里会失败。新实现会在创建任务前提示,并给出建议改用的绝对路径;自动选择解释器时 Homebrew 优先,因为 /usr/bin/python3 只是 CLT 的 shim。
  • 读取有边界。 先按文件属性检查大小(上限 128 KiB),再最多读 limit + 1 字节。目录和 FIFO 直接拒绝,FIFO 会卡住主线程。UTF-16、二进制、非 UTF-8 各给一条明确的错误提示,不用 GB18030 或有损解码去猜编码,因为脚本里的乱码比报错更危险。
  • 界面更克制。 模板网格原来有一个空位,就放在这里作为第 8 张卡片;表单最多多出一行说明,不加新的分区或警告块。
  • README 只描述功能本身,不带 fork 相关的内容。

设计和评审记录在 plans/011-import-local-script.md。再次感谢,欢迎试用新版后继续反馈。

@RuochenLyu RuochenLyu closed this Nov 25, 2026
@zrtnb6

zrtnb6 commented Nov 25, 2026

Copy link
Copy Markdown
Author

感谢你的评审意见,你的设计确实更好也更严谨 —— 内容嗅探的误判、缩进的 #!、先读全文件再查大小、GB18030 兜底解码,以及 launchd PATH 下 #!/usr/bin/env node 会失败这几点,我回头核对了自己的实现,全部成立。

我的本意只是想为项目做一点贡献。方便的话,能否在 plans/011-import-local-script.md 或 v1.7.0 的 release notes 里挂上 @zrtnb6?不方便也没关系,感谢 🙏

@RuochenLyu

Copy link
Copy Markdown
Owner

当然可以,已经加上了:

这个功能是你最先提出并动手实现的,署名理所应当。谢谢你的贡献,也谢谢你认真核对评审意见 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants