Laan's steps..

Feed Rss

Using conditional compilation[From adobe]

03.14.2009, flex, by .

from adobe livedoc:http://livedocs.adobe.com/flex/3/html/help.html?content=compilers_21.html#246171

To include or exclude blocks of code for certain builds, you can use conditional compilation. The mxmlc compiler lets you pass the values of constants to the application at compile time. Commonly, you pass a Boolean that is used to include or exclude a block of code such as debugging or instrumentation code. The following example conditionalizes a block of code by using an inline constant Boolean:

CONFIG::debugging {
    // Execute debugging code here.
}

To pass constants, you use the compiler.define compiler option. The constant can be a Boolean, String, or Number, or an expression that can be evaluated in ActionScript at compile time. This constant is then accessible within the application source code as a global constant.

To use the define option, you define a configuration namespace for the constant, a variable name, and a value using the following syntax:

-define=namespace::variable_name,value

The configuration namespace can be anything you want. The following example defines the constant debugging with a value of true in the CONFIG namespace:

-define=CONFIG::debugging,true

To set the values of multiple constants on the command-line, use the define

option more than once; for example:

mxmlc -define=CONFIG::debugging,true -define=CONFIG::release,false MyApp.mxml

To set the value of these constants in the flex-config.xml file, rather than on the command line, you write this as the following example shows:

<compiler>
    <define>
        <name>CONFIG::debugging</name>
        <value>true</value>
    </define>
    <define>
        <name>CONFIG::release</name>
        <value>false</value>
    </define>
</compiler>

In a Flex Ant task, you can set constants with a define element, as the following example shows:

<mxmlc ... >
    <define name="CONFIG::debugging" value="true"/>
    <define name="CONFIG::release" value="false"/>
</mxmlc>

Using inline constants

You can use inline constants in ActionScript. Boolean values can be used to conditionalize top-level definitions of functions, classes, and variables, in much the same way you would use an #IFDEF preprocessor command in C or C++. You cannot use constant Boolean values to conditionalize metadata or import statements.

The following example conditionalizes which class definition the compiler uses when compiling the application:

// compilers/MyButton.as
package  {
    import mx.controls.Button;

    CONFIG::debugging
    public class MyButton extends Button {    
        public function MyButton() {
            super();
            // Set the label text to blue.
            setStyle("color", 0x0000FF);
        }
    }

    CONFIG::release
    public class MyButton extends Button {    
        public function MyButton() {
            super();
            // Set the label text to red.
            setStyle("color", 0xFF0000);
        }
    }
}

You can also pass Strings and Numbers to the application and use them as inline constants, in the same way you might use a #define directive in C or C++. For example, if you pass a value named NAMES::Company, you replace it with a constant in your application by using an ActionScript statement like the following example shows:

private static const companyName:String = NAMES::Company;

Passing expressions

You can pass expressions that can be evaluated at compile time as the value of the constant. The following example evaluates to false:

-define+=CONFIG::myConst,"1 > 2"

The following example evaluates to 3:

-define+=CONFIG::myConst,"4 - 1"

Expressions can contain constants and other configuration values; for example:

-define+=CONFIG::bool2,false -define+=CONFIG::and1,"CONFIG::bool2 && false"

In general, you should wrap all constants with double quotes, so that the mxmlc compiler correctly parses them as a single argument.

Passing Strings

When passing Strings, you must add extra quotes to ensure that the compiler parses them correctly.

To define Strings on the command-line, you must surround them with double-quotes, and either escape-quote them (“”Adobe Systems”" or “‘Adobe Systems’”) or single-quote them (“‘Adobe Systems’”).

The following example shows both methods of including Strings on the command line:

-define+=NAMES::Company,"'Adobe Systems'" -define+=NAMES::Ticker,""ADBE""

To define Strings in configuration files, you must surround them with single or double quotes; for example:

<define>
    <name>NAMES::Company</name>
    <value>'Adobe Systems'</value>
</define>
<define>
    <name>NAMES::Ticker</name>
    <value>"ADBE"</value>
</define>

To pass empty Strings on the command line, use single quotes surrounded by double quotes, as the following example shows:

-define+=CONFIG::debugging,"''"

To pass empty Strings in configuration files, use double quotes (“”) or single quotes ().

Using conditional compilation[From adobe] 有1条回应

  1. 今天翻了你的 blog 翻了一天,写得很好。学到不少东西。
    这一篇文章我之前翻译了一下,放在我自己的博客上了,在这里留个足迹,有兴趣的朋友可以跟过来读中文版。
    http://blog.laiyonghao.com/2009/08/programming/flexas-3/365

    回复

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>