Mogenerator参数如何工作?我可以通过Xcode发送哪些参数?

68

Mogenerator的帮助信息非常少。所有这些参数都是干什么用的?

4个回答

149

这些参数既适用于命令行工具,也适用于Xcode:

  • --base-class:基类的名称,"private class"(例如_MyObject.h)将从中继承。这也会在同一个.h文件中添加一个形如#import "MyManagedObject.h"的导入语句。提示:如果要继承的类位于库中,则默认的导入语句将不起作用。作为解决方法,您可以为每个创建的项目增加额外的继承层级,并使该类从库中继承(例如,将基类设置为手动创建并从MyLibManagedObject继承的MyProjectManagedObject)。
  • --template-path:4个.motemplate文件所在的路径。如果未提供此参数,它将查找所有的"app support directories"(例如"/Library/Application Support/mogenerator/")。
  • --template-group:使用template-path目录下的子目录名称。
  • --template-var arc=true:生成的文件必须使用ARC编译,此参数必需
  • --output-dir:所有生成文件的输出目录。
  • --machine-dir:将输出_<class>.h_<class>.m的目录。如果也定义了--output-dir,则此参数优先。
  • --human-dir:将输出<class>.h<class>.m的目录。如果也定义了--output-dir,则此参数优先。
  • --includem:包含所有.h文件的#import的文件的完整路径。此文件不需要存在(即,如果不存在则会为您创建)。该文件不会自动包含在项目中。您必须手动将其拖入项目的"Groups & Files"列表中。

在Xcode中使用相对路径来设置任何参数都不会生效,因为工作目录被设置为系统的根目录之一(例如Applications、Developer、Library或System),我还没有足够的时间找出它到底是哪一个。

Xcode中不能使用的参数:

  • --model:.xcdatamodel文件的路径,无法在Xcode中设置。
  • --list-source-files
  • --orphaned
  • --versioned
  • --help

通过Xcode运行并发送参数到xmod:

(更新:我还没有在Xcode 4上尝试过这个方法,只在Xcode 3上尝试过。对于Xcode 4,您可以将mogenerator添加为构建阶段,而不是按照以下步骤进行操作。)

  1. 转到.xcdatamodel文件的信息页面。
  2. 选择注释选项卡。
  3. 在注释字段中单独一行添加xmod
  4. 每次保存模型时,它都会为您生成机器文件。

要发送参数,它们必须位于自己的行上:

这个有效:

xmod
--base-class CLASS
--template-path PATH

甚至这也可以工作:

xmod
--base-class CLASS --template-path PATH

但是,这样做不起作用:

xmod --base-class CLASS --template-path PATH

注意:为使设置生效,您必须关闭信息窗口。

13
干得好,我会将这个链接放在项目主页上 :-) - rentzsch
2
注意:如果您通过Homebrew安装了mogenerator,则它将位于/usr/local/bin中,而这不在Xcode的默认路径中。为使其正常工作,请将PATH=$ {PATH}:/usr/local/bin添加为脚本的第一行。 - Josh at The Nerdery
2
可能值得添加 --template-var arc=true,这是生成的代码与 ARC 兼容所必需的。 - combinatorial
1
似乎找不到.xcdatamodel文件的信息页面。有关如何进入该页面的截图吗? - Tony
“-O” 是 “--output-dir” 的别名吗? - SimplGy
显示剩余3条评论

6
从XCode 4开始,信息窗口不再可用,所以如果您无法按照上面的答案进行设置,请不要担心。
请使用John Blanco的指南设置脚本目标,该目标允许您直接将命令行参数传递给mogenerator。请注意,您可能需要稍微调整他示例中的路径...在脚本中加入pwd,并检查路径是否与脚本的工作目录相匹配,如果不能立即运行,请检查一下。
要获取可用的命令行参数列表,请在Terminal中运行mogenerator --help。据我所知,所有命令都可以从脚本步骤中执行。
如果您想自动重建每个构建的机器文件,请参见这个答案,了解通过“预操作”调用mogenerator的另一种方法。还有一个很好的技巧,可以将mogenerator脚本放入您的VCS中。

非常好的答案。谢谢你。我从这里链接到了它:https://dev59.com/gnA65IYBdhLWcg3w7DP8#16788847 - Git.Coach

1

以下是版本1.27的--help输出

mogenerator: Usage [OPTIONS] <argument> [...]

  -m, --model MODEL             Path to model
  -C, --configuration CONFIG    Only consider entities included in the named configuration
      --base-class CLASS        Custom base class
      --base-class-import TEXT        Imports base class as #import TEXT
      --base-class-force CLASS  Same as --base-class except will force all entities to have the specified base class. Even if a super entity exists
      --includem FILE           Generate aggregate include file for .m files for both human and machine generated source files
      --includeh FILE           Generate aggregate include file for .h files for human generated source files only
      --template-path PATH      Path to templates (absolute or relative to model path)
      --template-group NAME     Name of template group
      --template-var KEY=VALUE  A key-value pair to pass to the template file. There can be many of these.
  -O, --output-dir DIR          Output directory
  -M, --machine-dir DIR         Output directory for machine files
  -H, --human-dir DIR           Output directory for human files
      --list-source-files       Only list model-related source files
      --orphaned                Only list files whose entities no longer exist
      --version                 Display version and exit
  -h, --help                    Display this help and exit

Implements generation gap codegen pattern for Core Data.
Inspired by eogenerator.

0
可能会有帮助。用于确定可以使用哪些参数的。
--template-var KEY=VALUE

打开*.motemplate文件,查找类似于“TemplateVar.”的字符串。在点号后面您将看到参数名称,并能够理解它的作用。
此参数具有内置模板。
--template-var arc=true 
--template-var frc=true
--template-var modules=true

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接