【问题标题】:Rubymotion - Multiple targets with same codeRubymotion - 具有相同代码的多个目标
【发布时间】:2014-05-08 00:46:39
【问题描述】:

前段时间,我不得不使用 XCode 创建同一个应用程序的两个版本(例如,一个 Lite 和一个 Paid)。这很容易,添加一个带有预处理器定义的第二个目标并在代码中使用

#if defined(SOME_DEFINE)
  [some code];
#else
  [some otherCode];
#endif

我现在必须使用 Rubymotion 做同样的事情。最好的方法应该是什么?

【问题讨论】:

    标签: objective-c rubymotion


    【解决方案1】:

    Laurent 在这里回答了这个问题:https://twitter.com/lrz/status/464079142065930240,我想我会稍微解释一下。

    所以我们的想法是生成一个包含常量的 .rb 文件,然后编译该文件并且这些常量在您的应用程序中可用。如何确定值取决于您,但我将使用解析 ENV 变量的示例,例如

    rake some_define=true
    

    在您的 Rakefile 中:

    Motion::Project::App.setup do |app|
      if ENV['some_define'] == 'true' || ENV['some_define'] == '1'
        some_define = true
      else
        some_define = false
      end
    
      constants_contents = "SOME_DEFINE = #{some_define.inspect}\n"
      File.open('app/.constants.rb', 'w') do |file|
        file.write(constants_contents)
      end
    
      # add this config file to the beginning of app.files, so you can
      # use the constant anywhere
      app.files.insert(0, 'app/.constants.rb')
    end
    

    所以现在您的应用中将有 SOME_DEFINE 可用;不如#define 宏优雅,但最终结果几乎相同。

    如果[some code][some otherCode] 的数量很大,您应该将它们放在单独的文件中,然后您可以有条件地包含这些文件。在这种情况下,不要将它们放在app/ 中,将它们放在platform/ 或类似的地方,然后:

    Motion::Project::App.setup do |app|
      if ENV['some_define'] == 'true' || ENV['some_define'] == '1'
        app.files.insert(0, 'platform/special.rb')
      else
        app.files.insert(0, 'platform/default.rb')
      end
    end
    

    【讨论】:

    • 感谢 Laurent 推文的解释。我想这是处理这个问题的最红宝石方式:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-11
    • 2021-06-19
    • 2014-10-14
    • 2016-05-06
    • 1970-01-01
    相关资源
    最近更新 更多