如何将 ruby​​ 包捆绑到 [Objective-C] cocoa 应用程序中?

发布于 2024-09-06 09:50:52 字数 684 浏览 5 评论 0原文

我试图弄清楚如何将用 Ruby (Sass) 编写的包捆绑到一个供我执行的 Cocoa 应用程序(Objective-C,而不是 Ruby Cocoa)(通过 NSTask 很好,除非有一种我不知道的桥接 ObjC<->Cocoa 的简单方法)。

Sass 包是您必须使用“gem install”或“rake install”来安装的东西——这样做会将大量文件放入我的 ~/.gem 目录中。由于我希望安装了基于 Cocoa 的应用程序的任何人都能够从我的应用程序中执行此工具,因此我不希望用户经历安装任何其他内容的过程,因此我希望能够将我需要的所有内容嵌入到我的应用程序包的 Resources 目录中。

然而,由于不太熟悉 Ruby 的内部结构和结构(抱歉,我很难记住 ObjC/Cocoa!),我不清楚 1,444 个文件中的哪一个安装在 ~/.gem 目录中的 (是的,我数过) 我需要嵌入到应用程序中,以及我可能需要做什么才能使目录引用等正常工作。

如果有人有将 ruby​​ 工具嵌入到 Cocoa 应用程序中的经验,我将非常感谢您的意见。考虑到 ruby​​ 安装在 Mac OS X 上,我没想到这会如此困难......但显然这个包(通常?非典型?)不仅仅是一个脚本文件。 ..

I'm trying to figure out how I could bundle a package, written in Ruby (Sass) into a Cocoa application (Objective-C, not Ruby Cocoa) for me to execute (via NSTask is fine, unless there is an easy way to bridge ObjC<->Cocoa that I'm not aware of).

The Sass package is something you have to install, using "gem install" or "rake install" -- doing so puts a ton of files in my ~/.gem directory. Since I want anybody who has installed my Cocoa-based application to be able to execute this tool from within my app, I don't want to have the user go through a process of installing anything additional, so I'm hoping to be able to embed everything I need in the Resources directory of my app package.

However, not being that familiar with the internals and structure of Ruby (Sorry, I'm having trouble just keeping ObjC/Cocoa in my head!), It's not clear to me just which of the 1,444 files that got installed in the ~/.gem directory (Yes, I counted) I need to embed in the application and what I might need to do to get the directory references, etc. working right.

If anybody has any experience with embedding a ruby tool into a Cocoa application, I would really appreciate your input. I wasn't expecting this to be so hard, considering that ruby is installed on Mac OS X ... but apparently this package is (typically? atypically?) more than just a single script file....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

娇俏 2024-09-13 09:50:52

您可以将 gems 安装到特定位置:

GEM_HOME=path/to/your/project/gems gem install sass

然后,作为构建过程的一部分,将该文件夹复制到您的资源中。当你想运行 sass 时,找到你的 gem 文件夹。像这样调用 ruby​​:(

NSString *gems_path = …;
NSTask *task = [[NSTask alloc] init];
// you may want to copy NSProcessInfo.processInfo.environment and modify it instead
task.environment = @{ @"GEM_HOME": gems_path };
task.launchPath = @"/usr/bin/ruby";
task.arguments = @[[gems_path stringByAppendingPaths:@[@"bin",@"sass"]], @"rest", @"of", @"your", @"arguments"];
// add handling for I/O
[task launch];

在 github 中输入,可能有愚蠢的错误)

请注意,您可能还想捆绑 ruby​​(也许是 macruby),以防止兼容性问题。如果没有,请确保测试您支持的所有 OS X 版本,尤其是 10.9,因为 ruby​​ 已升级到 2.0。

You can install gems into a specific location:

GEM_HOME=path/to/your/project/gems gem install sass

Then, as part of your build process, copy the folder into your resources. When you want to run sass, find your gem folder. Call ruby like so:

NSString *gems_path = …;
NSTask *task = [[NSTask alloc] init];
// you may want to copy NSProcessInfo.processInfo.environment and modify it instead
task.environment = @{ @"GEM_HOME": gems_path };
task.launchPath = @"/usr/bin/ruby";
task.arguments = @[[gems_path stringByAppendingPaths:@[@"bin",@"sass"]], @"rest", @"of", @"your", @"arguments"];
// add handling for I/O
[task launch];

(Typed into github, may have silly mistakes)

Note that you may want to bundle ruby as well (perhaps macruby), to prevent compatibility issues. If not, ensure that you test all versions of OS X that you support, especially 10.9, as ruby was upgrade to 2.0.

所有深爱都是秘密 2024-09-13 09:50:52

主要感谢 @Jacob Lukas,我发现只需在 gem install< 上设置 GEM_HOME /code> 未正确附加依赖项。因此,对于我的情况 - 我只需要运行在 Xcode 插件中生成的脚本 - 我最终得到:

gem install -i ~/xCodeProjects/PluginOne/gems xcodeproj --verbose

获取宝石 &依赖关系。然后,我用了:

NSString *gems_path = [[bundle resourcePath] stringByAppendingString:@"/gems"];

NSTask *task = [[NSTask alloc] init];
task.environment = @{ @"GEM_HOME" : gems_path };
task.launchPath = @"/System/Library/Frameworks/Ruby.framework/Versions/Current/usr/bin/ruby";
task.arguments = @[generatedRubyFilePath];

[task launch];

Major thanks to @Jacob Lukas, I found that simply setting GEM_HOME on gem install <gem> didn't correctly attach the dependencies. So, for my case--where I just need to run a script I'm generating in an Xcode plugin--I ended up with:

gem install -i ~/xCodeProjects/PluginOne/gems xcodeproj --verbose

to get the gem(s) & dependencies. Then, I used:

NSString *gems_path = [[bundle resourcePath] stringByAppendingString:@"/gems"];

NSTask *task = [[NSTask alloc] init];
task.environment = @{ @"GEM_HOME" : gems_path };
task.launchPath = @"/System/Library/Frameworks/Ruby.framework/Versions/Current/usr/bin/ruby";
task.arguments = @[generatedRubyFilePath];

[task launch];
筱武穆 2024-09-13 09:50:52

我在使用 Ruby gems 做类似的事情时遇到了困难,所以我将分享对我有用的东西,希望它能帮助将来的人。

第 1 步:安装 Gems

首先将您想要使用的 gems 安装到您可以轻松找到的文件夹中,例如项目文件夹中的 gems 文件夹。正如 Stan 在他的回答中所示,使用 -i 选项并在安装 gem 时提供 gems 文件夹的路径。

gem install -i /path/to/gem/folder gemName

步骤 2:添加“复制文件”构建阶段

通过选择“编辑器”>“添加复制文件”构建阶段到您的应用程序目标。添加构建阶段>添加复制文件构建阶段。使用“资源”菜单选择应用程序包中复制 gem 的位置。如果您希望 gem 位于其自己的文件夹中,请在子路径文本字段中添加文件夹名称。

单击添加按钮添加文件。打开一张纸。单击添加其他按钮。导航到您的 gem 文件夹位置并选择您的 gem 文件夹。
现在,当您构建项目时,Xcode 会将 gems 复制到应用程序包中。

步骤 3:找到应用程序包中的 gems 文件夹

使用 Bundle 类查找包中的标准位置,例如 Resources 文件夹。构建 gems 文件夹的路径。运行 gem 时,您将需要它作为环境变量。

包含 gem 的文件夹应该有一个包含可执行文件的 bin 文件夹。您将构建可执行文件的路径,并在运行 gem 时使用该路径作为第一个参数。

步骤 4:配置运行 gem 的命令

使用 Process 类(以前称为 NSTask)来运行命令行程序,例如 gem。首先创建一个 Process 对象。

let taskToRun = Process()

将启动路径设置为 Ruby 解释器的路径。

taskToRun.launchPath = "/usr/bin/ruby"

将环境变量 GEM_HOME 设置为 gems 文件夹的路径。

taskToRun.environment = ["GEM_HOME" : gemsPath]

使用参数列表设置 arguments 属性。第一个参数应该是 gem 可执行文件的路径。

let argumentList = [gemExecutablePath, inputURL.path, "-o", outputURL.path]
taskToRun.arguments = argumentList

实际的参数列表取决于您使用的 gem。

步骤 5:运行 gem

在 macOS 10.13 及更高版本上,调用 run 函数来运行 gem。

do {
    try taskToRun.run()
} catch {
    fatalError("Error running command line tool.")
}

在早期版本的 macOS 上,调用函数 launch 来运行 gem。

taskToRun.launch()

您可以在以下文章中找到更多详细信息:

在 Mac 应用程序中使用 Ruby Gem

I had trouble doing something similar with Ruby gems so I'll share what worked for me in the hopes that it helps someone in the future.

Step 1: Install the Gems

Start by installing the gems you want to use into a folder you can easily locate, such as a gems folder inside your project folder. As Stan showed in his answer, use the -i option and supply the path to the gems folder when installing the gem.

gem install -i /path/to/gem/folder gemName

Step 2: Add a Copy Files build phase

Add a Copy Files build phase to your app target by choosing Editor > Add Build Phase > Add Copy Files Build Phase. Use the Resources menu to choose a place to copy the gems in the app bundle. If you want the gems in their own folder, add the folder name in the Subpath text field.

Click the Add button to add the files. A sheet opens. Click the Add Other button. Navigate to your gem folder location and select your gem folder.
Now when you build the project, Xcode will copy the gems into the app bundle.

Step 3: Locate the gems folder in the app bundle

Use the Bundle class to find standard locations in the bundle, such as the Resources folder. Build a path to the gems folder. You will need it as an environment variable when running the gem.

The folder containing the gems should have a bin folder containing the executable files. You would build a path to the executable file and use that path as the first argument when running the gem.

Step 4: Configure the command to run the gem

Use the Process class, formerly NSTask, to run a command-line program, such as a gem. Start by creating a Process object.

let taskToRun = Process()

Set the launch path to the path of the Ruby interpreter.

taskToRun.launchPath = "/usr/bin/ruby"

Set the environment variable GEM_HOME to the path of your gems folder.

taskToRun.environment = ["GEM_HOME" : gemsPath]

Set the arguments property with a list of arguments. The first argument should be the path to the gem's executable file.

let argumentList = [gemExecutablePath, inputURL.path, "-o", outputURL.path]
taskToRun.arguments = argumentList

The actual list of arguments depends on the gem you're using.

Step 5: Run the gem

On macOS 10.13 and later, call the run function to run the gem.

do {
    try taskToRun.run()
} catch {
    fatalError("Error running command line tool.")
}

On earlier versions of macOS, call the function launch to run the gem.

taskToRun.launch()

You can find more detailed information in the following article:

Using a Ruby Gem in a Mac App

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文