<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Maksym Grebenets on Medium]]></title>
        <description><![CDATA[Stories by Maksym Grebenets on Medium]]></description>
        <link>https://medium.com/@maksymgrebenets?source=rss-241c28b5e61b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*d34aIi0SMoNtA2OO.jpg</url>
            <title>Stories by Maksym Grebenets on Medium</title>
            <link>https://medium.com/@maksymgrebenets?source=rss-241c28b5e61b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 09:41:42 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@maksymgrebenets/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Continuous Integration with the TeamCity Kotlin REST Client]]></title>
            <link>https://heartbeat.comet.ml/continuous-integration-with-the-teamcity-kotlin-rest-client-c4f7d1c217e4?source=rss-241c28b5e61b------2</link>
            <guid isPermaLink="false">https://medium.com/p/c4f7d1c217e4</guid>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[continuous-integration]]></category>
            <category><![CDATA[heartbeat]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[kotlin]]></category>
            <dc:creator><![CDATA[Maksym Grebenets]]></dc:creator>
            <pubDate>Mon, 15 Jul 2019 14:19:05 GMT</pubDate>
            <atom:updated>2021-10-18T15:57:25.420Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*b766MTDmuHf7vNaG77_7YA.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@realaxer?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">tian kuan</a> on <a href="https://unsplash.com/search/photos/pipes?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>Whichever CI server you use for development, being able to access it programmatically via REST API is an essential capability for implementing all kinds of automation. In this article, you’ll learn how to utilize the <a href="https://www.jetbrains.com/teamcity/">TeamCity</a> REST API using the <a href="https://github.com/JetBrains/teamcity-rest-client">TeamCity Kotlin REST Client</a>.</p><p>Let’s start by setting up a primary goal.</p><blockquote><em>I want to be able to access my TeamCity server using the Kotlin programming language.</em></blockquote><p>To help us achieve this goal, JetBrains provides a <a href="https://github.com/JetBrains/teamcity-rest-client">REST client</a> (mentioned above). So all I have to do is write a simple Kotlin script that uses the REST client and implement whichever automation I want, right?</p><p>Well…in my case, I’m not a Java or Android developer and building Kotlin source code is totally new to me, so this article will cover some basics.</p><h3>⚙️ Setup</h3><p>I’m working on a Mac OS X machine, so I’ll start by installing the <a href="https://gradle.org/">Gradle Build Tool</a>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/998f5abce5cbb86aa33294d797f6a9d7/href">https://medium.com/media/998f5abce5cbb86aa33294d797f6a9d7/href</a></iframe><p>Next, I’ll create a new directory for my project as well as the main Kotlin file and a Gradle wrapper.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/82c61e5cf428a8597f11aa4cda92ffd1/href">https://medium.com/media/82c61e5cf428a8597f11aa4cda92ffd1/href</a></iframe><p>From now on, I should be running all Gradle commands using the wrapper, e.g. ./gradlew build. The wrapper will be saved in the gradle/wrapper folder. This folder can be committed to source control, which removes the dependency on global Gradle installation and makes the project self-contained.</p><p>I want to use Kotlin to write build scripts, so I create a build.gradle.kts file in the project root with the following contents:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d418959ae186235ced5e8b961bb4adf1/href">https://medium.com/media/d418959ae186235ced5e8b961bb4adf1/href</a></iframe><p>This is to make sure I’m using an exact version of Gradle and it doesn’t auto-update.</p><h3>🔨 Implement</h3><p>This may sound unusual, but in this case I choose to write the Kotlin code first and then make it compile and run. Based on the example on the REST client project page, I came up with the following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8ff0f5e88bbafe1273ccaeae5360c49c/href">https://medium.com/media/8ff0f5e88bbafe1273ccaeae5360c49c/href</a></iframe><p>In a nutshell, this code connects to TeamCity using a username and password provided as input arguments, then fetches the latest build information for a specified build configuration and prints it out to the console. Check the inline comments for more details.</p><h3>👷‍♂️ Build</h3><p>After a lot of trial an error, I came up with the the following build file:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c28dc1a7b24e0e405e6d7566062c5e9d/href">https://medium.com/media/c28dc1a7b24e0e405e6d7566062c5e9d/href</a></iframe><p>Check the inline comments for detailed information. This build script builds a Kotlin application targeted for running on top of <a href="https://en.wikipedia.org/wiki/Java_virtual_machine">JVM</a>. The application depends on the TeamCity REST client.</p><blockquote>Note that you have to install Java version 1.8 to build and run the application.</blockquote><p>It’s time to build it:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6eac0fee15c8b583ff177136c5be2c32/href">https://medium.com/media/6eac0fee15c8b583ff177136c5be2c32/href</a></iframe><h3>🏃‍♀️ Run</h3><p>I can now run the Kotlin application:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e599d096d0ebf19be4d0dd5b96c4d9e0/href">https://medium.com/media/e599d096d0ebf19be4d0dd5b96c4d9e0/href</a></iframe><p>After customizing input arguments, I’m able to see something like this in the logs:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/44df8b81eefe6512baa038b854340599/href">https://medium.com/media/44df8b81eefe6512baa038b854340599/href</a></iframe><p>That’s just the beginning! I can now use Kotlin, a nice modern programming language, to automate various CI/CD tasks on TeamCity.</p><p>For a full example project see <a href="https://github.com/mgrebenets/teamcity-rest-client-example">this repository</a>.</p><p><em>Editor’s Note: </em><a href="https://heartbeat.comet.ml/"><em>Heartbeat</em></a><em> is a contributor-driven online publication and community dedicated to providing premier educational resources for data science, machine learning, and deep learning practitioners. We’re committed to supporting and inspiring developers and engineers from all walks of life.</em></p><p><em>Editorially independent, Heartbeat is sponsored and published by </em><a href="http://comet.ml/?utm_campaign=heartbeat-statement&amp;utm_source=blog&amp;utm_medium=medium"><em>Comet</em></a><em>, an MLOps platform that enables data scientists &amp; ML teams to track, compare, explain, &amp; optimize their experiments. We pay our contributors, and we don’t sell ads.</em></p><p><em>If you’d like to contribute, head on over to our</em><a href="https://heartbeat.fritz.ai/call-for-contributors-october-2018-update-fee7f5b80f3e"><em> call for contributors</em></a><em>. You can also sign up to receive our weekly newsletters (</em><a href="https://www.deeplearningweekly.com/"><em>Deep Learning Weekly</em></a><em> and the </em><a href="https://info.comet.ml/newsletter-signup/"><em>Comet Newsletter</em></a><em>), join us on</em><a href="https://join.slack.com/t/fritz-ai-community/shared_invite/enQtNTY5NDM2MTQwMTgwLWU4ZDEwNTAxYWE2YjIxZDllMTcxMWE4MGFhNDk5Y2QwNTcxYzEyNWZmZWEwMzE4NTFkOWY2NTM0OGQwYjM5Y2U"><em> </em></a><a href="https://join.slack.com/t/cometml/shared_invite/zt-49v4zxxz-qHcTeyrMEzqZc5lQb9hgvw"><em>Slack</em></a><em>, and follow Comet on </em><a href="https://twitter.com/Cometml"><em>Twitter</em></a><em> and </em><a href="https://www.linkedin.com/company/comet-ml/"><em>LinkedIn</em></a><em> for resources, events, and much more that will help you build better ML models, faster.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c4f7d1c217e4" width="1" height="1" alt=""><hr><p><a href="https://heartbeat.comet.ml/continuous-integration-with-the-teamcity-kotlin-rest-client-c4f7d1c217e4">Continuous Integration with the TeamCity Kotlin REST Client</a> was originally published in <a href="https://heartbeat.comet.ml/">Heartbeat</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Xcode Build Settings in Depth]]></title>
            <link>https://heartbeat.comet.ml/xcode-build-settings-in-depth-733667d01a9a?source=rss-241c28b5e61b------2</link>
            <guid isPermaLink="false">https://medium.com/p/733667d01a9a</guid>
            <category><![CDATA[ios-app-development]]></category>
            <category><![CDATA[xcode]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[heartbeat]]></category>
            <dc:creator><![CDATA[Maksym Grebenets]]></dc:creator>
            <pubDate>Tue, 02 Jul 2019 14:13:13 GMT</pubDate>
            <atom:updated>2021-09-28T18:06:30.817Z</atom:updated>
            <content:encoded><![CDATA[<h4>Getting to the rock bottom of Xcode build settings</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xZ_jPHoNUqAgnYxo713E8A.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@mikael_k?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Mikael Kristenson</a> on <a href="https://unsplash.com/search/photos/wrench?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>If you’ve ever done any MacOS or iOS development, you’ve also eventually had to deal with Xcode build settings.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*vro7WfKmzu5SohESPrak3A.png" /></figure><p>So what are those and what do we know about them?</p><p>For a standard iOS project, there are roughly 500 build settings grouped into around 50 categories. These build settings control virtually every single aspect of how your app is built and packaged. At the very least, build settings are what make Debug build so different from Release build.</p><p>In this article, I’ll mostly focus on build settings that control the behavior of Apple Clang and Swift compilers and the linker.</p><h3>🗄 Background Check</h3><p>Build settings are not as simple as they may seem.</p><p>For starters, there are project- and target-level build settings as well as default OS settings.</p><p>The default OS build settings are inherited on a project level and project-level build settings are inherited on a target level.</p><p>Then there are .xcconfig files, which can be used to represent build settings in plain text format. The xcconfigs can be set on target and project level and add two more levels to inheritance flow.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*Y1kN6h7IPwm_gOp6CGdppg.png" /></figure><p>To make things even more complicated, you can include other xcconfigs using C-like #include statements.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dd53a69957f7c538f073a993e8fc4548/href">https://medium.com/media/dd53a69957f7c538f073a993e8fc4548/href</a></iframe><p>But there’s much more to build settings and xcconfigs.</p><p>To get familiar with all of the above, I’d highly recommend starting with <a href="https://pewpewthespells.com/blog/xcconfig_guide.html">The Unofficial Guide to xcconfig files</a>. Check out the rest of <a href="https://pewpewthespells.com/ramble.html">this amazing blog</a> for even more hands-on information about understanding and managing build settings.</p><h3>⬇️ Level Down</h3><p>Now let’s go one more level down. When it comes to compiling and linking the source code, the Xcode build system manages 3 main tools under the hood:</p><ul><li>Clang Cxx compiler for compiling C/C++ and Objective-C/C++ code</li><li>Swift compiler</li><li>Linker to link all object files together</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*HkOfq7jlJH4hOFI0GrxV9w.png" /></figure><p>Each of those tools has its own set of command line flags.</p><p>Clang compiler and linker flags are <a href="https://clang.llvm.org/docs/ClangCommandLineReference.html">documented here</a>. The Swift compiler swift --help command provides a list of Swift command line flags. Surprisingly, I failed to find online documentation similar to Clang.</p><p>When a build setting is set in Xcode UI, Xcode then translates it to the appropriate flags for underlying tools.</p><p>For example, setting GCC_TREAT_WARNINGS_AS_ERRORS to YES will add a-Werror flag for the Clang Cxx compiler.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*U-jmAi9nsiiLI1BHLtlqsA.png" /></figure><p>Similarly, setting SWIFT_TREAT_WARNINGS_AS_ERRORS to YES will <br>add a -warnings-as-errors flag to all invocations of the Swift compiler.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*ieqFEdHs4EBuRpOXZzWWyg.png" /></figure><p>Finally, some build settings are translated into flags for all three tools; for example, enabling code coverage using the <br>CLANG_ENABLE_CODE_COVERAGE = YES build setting will translate into the following<em>*</em>:</p><ul><li>-profile-instr-generate and -fcoverage-mapping for the Clang compiler</li><li>-profile-coverage-mapping and -profile-generate for the Swift compiler</li><li>-fprofile-instr-generate for linker</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*aCozFi55tvX7fgWY4trkag.png" /></figure><p>— — —</p><blockquote>*Technically, it takes more than just setting CLANG_ENABLE_CODE_COVERAGE to enable code coverage, more details to come.</blockquote><p>— — —</p><p>So, how does Xcode know which flags to map build settings to? Where is this mapping information stored, and can it be extracted?</p><h3>⚙️ Xcode Specs</h3><p>The answer to the questions above is Xcode Specs or <em>xcspecs</em>.</p><p>Xcspecs are ASCII plist files with .xcspec file extensions stored deep inside the Xcode.app bundle.</p><p>Xcode uses these specs to render build settings UI and to translate build settings to command line flags.</p><p>There are xcspecs for the Clang compiler (<strong>Clang LLVM 1.0.xcspec</strong>), the Swift compiler (<strong>Swift.xcspec</strong>), and linker (<strong>Ld.xcspec</strong>), as well as a number of xcspecs for core build system and other tools.</p><p>These xcspecs reference each other and work together as a system. Each xcspec contains a specification for one or more <em>tools.</em> For example, Swift xcspec contains specification for the Swift compiler tool, while Clang LLVM xcspec contains specifications for the Clang compiler, analyzer, a couple of migrators, and an <a href="https://en.wikipedia.org/wiki/Abstract_syntax_tree">AST</a> builder tool.</p><p>A tool specification includes such details as its name, description, identifier, executable path, supported file types, and more.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/83cb36920259870e09482c1a00db1e69/href">https://medium.com/media/83cb36920259870e09482c1a00db1e69/href</a></iframe><h3>🔘 Options</h3><p>In context of this article, we’re most interested in the<strong>Options</strong> array entry of the tool’s specification, which is where information about build settings is stored.</p><p>For example, the value of SWIFT_EXEC is stored as an option and is used by Xcode to resolve the ExecPath = &quot;$(<strong>SWIFT_EXEC</strong>)&quot;; statement:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/101e1042229102a8bd12f5e3c72bf248/href">https://medium.com/media/101e1042229102a8bd12f5e3c72bf248/href</a></iframe><p>All build settings (options) have <strong>Name</strong> and <strong>Type</strong> properties.<strong>Name</strong> defines the build setting name, e.g. SWIFT_OPTIMIZATION_LEVEL.</p><p>Build settings may also have a <strong>Category</strong>, <strong>Description</strong>, and <strong>DisplayName</strong> properties. For example, the display name for SWIFT_OPTIMIZATION_LEVEL is <em>Optimization Level</em>.</p><p>A few build settings are hidden and never appear in Xcode UI. Some of them have a corresponding comment in the code like // Hidden, but not all.</p><h3>Types</h3><p>There are 6 different types of build settings:</p><ul><li><strong>String</strong></li><li><strong>StringList</strong></li><li><strong>Path</strong></li><li><strong>PathList</strong></li><li><strong>Enumeration</strong></li><li><strong>Boolean</strong></li></ul><h4>ℹ️ String and Path</h4><p>A build setting of <strong>String</strong> type has, well, a string value. Note that a string value doesn’t have to be quoted using double quotes.</p><p>As a matter of fact, some build settings that have integer values are represented using <strong>String</strong> type with the default value set to 0, but not to &quot;0&quot;.</p><p><strong>Path</strong> type is almost identical to <strong>String</strong> when used in xcspecs. My guess is that <strong>Path</strong> type build settings are handled differently with regards to escaping whitespaces and other special characters. They also get special treatment when resolving wildcard characters like <strong>*</strong>.</p><h4>ℹ️ StringList and PathList</h4><p><strong>StringList</strong> and <strong>PathList</strong> are used to represent a list of string and path values, respectively.</p><p>If not provided, the default value is an empty list. You can define a list type in Xcode UI because it allows multiple values input:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/679/1*yze-Soabi6gkSGF9CKzLUA.png" /></figure><h4>ℹ️ Enumeration</h4><p>Values of <strong>Enumeration</strong> type have a fixed list of values (cases), defined using a <strong>Values</strong> key, for example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/55f88b96f2d1d2e6f7eb6e6a5ee8420b/href">https://medium.com/media/55f88b96f2d1d2e6f7eb6e6a5ee8420b/href</a></iframe><p>Another good example is build settings that have a YES_AGGRESSIVE or YES_ERROR value on top of YES and NO. These build settings are declared as enumerations, too:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/34e9454ea3c53feadc3731cf57077968/href">https://medium.com/media/34e9454ea3c53feadc3731cf57077968/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/514/1*BRpRJiD2vbJaVPXvHqfD6w.png" /></figure><h4>ℹ️ Boolean</h4><p>Finally, values of <strong>Boolean</strong> type have either YES or NO values.</p><p>Note that only YES and NO are correct values for <strong>Boolean</strong> type, but not &quot;YES&quot; or &quot;NO&quot;.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/517/1*6uxok6qDae_I1hCv4_o0sg.png" /></figure><h3>🗺 Command Line Flags Mapping</h3><p>As we already know, a lot of build setting values map to different command line flags.</p><p>The mapping information is defined in xcspecs using one of the following keys:</p><ul><li><strong>CommandLineArgs</strong></li><li><strong>CommandLineFlag</strong></li><li><strong>CommandLinePrefixFlag</strong></li><li><strong>AdditionalLinkerArgs</strong></li></ul><h4>ℹ️ Command Line Arguments</h4><p><strong>CommandLineArgs</strong> key-value entry is used to map build setting value to a <em>list</em> of command line arguments.</p><h4>Scalar Types</h4><p>A good example is the SWIFT_MODULE_NAME build setting of String type:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f647657db482413fe9dbdab52209d05d/href">https://medium.com/media/f647657db482413fe9dbdab52209d05d/href</a></iframe><p>The $(value) is resolved to the current build setting value, e.g. the MyModule value is mapped to the-module-name &quot;MyModule&quot; Swift compiler flag.</p><h4>List Types</h4><p>For list types, each build setting value in the list is mapped to one or more command line flags. For example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/329b332326a2e2b5f17fb8baef1b88fa/href">https://medium.com/media/329b332326a2e2b5f17fb8baef1b88fa/href</a></iframe><p>If the value of CLANG_ANALYZER_OTHER_CHECKERS is &quot;checker1&quot; &quot;checker2&quot;, it will be mapped to the following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8d035067f47092a65c27d2447b34df15/href">https://medium.com/media/8d035067f47092a65c27d2447b34df15/href</a></iframe><h4>Enumeration Type</h4><p>For enumeration types, mapping is defined for each enumeration case. For example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5eb5d3cefb21db63fcb0f5e824bc1aef/href">https://medium.com/media/5eb5d3cefb21db63fcb0f5e824bc1aef/href</a></iframe><ul><li>ansi value is mapped to the -ansi compiler flag</li><li>compiler-default maps to no flags</li><li>All other enumeration values map to the -std=$(value) compiler flag. <br>Note the use of &quot;&lt;&lt;otherwise&gt;&gt;&quot;—this is similar to default: enum switch in C-like languages.</li></ul><h4>Boolean Type</h4><p><strong>Boolean</strong> type is mapped just like enumerations with two YES and NO cases.</p><h4>ℹ️ Command Line Flag</h4><p><strong>CommandLineFlag</strong> is used to prepend the command line flag to the value of the build setting.</p><p>For example, SDKROOT is defined like so:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a97a7cc3ef7c7a304679b78560d23328/href">https://medium.com/media/a97a7cc3ef7c7a304679b78560d23328/href</a></iframe><p>So if the value of SDKROOT is iphoneos, the corresponding Clang compiler flag will be -isysroot iphoneos.</p><p>In a way, <strong>CommandLineFlag</strong> is shorthand for using <strong>CommandLineArgs</strong>. I.e. in the SDKROOT example, <strong>CommandLineFlag</strong> = &quot;-isysroot&quot;; could be replaced with <strong>CommandLineArgs</strong> = (&quot;-isysroot&quot;, &quot;$(value)&quot;); .</p><p>Handling of list, <strong>Enumeration</strong>, and <strong>Boolean</strong> types is similar to handling <strong>CommandLineArgs</strong>, too.</p><p>For example, given the definition:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/55fc10761e822a1871daf5f479c7226d/href">https://medium.com/media/55fc10761e822a1871daf5f479c7226d/href</a></iframe><p>The value like SYSTEM_FRAMEWORK_SEARCH_PATHS = &quot;A&quot; &quot;B&quot; &quot;C&quot; will be mapped to:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f9b852967a58c2d440c0789d083a7135/href">https://medium.com/media/f9b852967a58c2d440c0789d083a7135/href</a></iframe><p>Enumerations deserve a special mention, because build flag mapping can be defined next to the value. A good example is MACH_O_TYPE:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6c43bd008582f23d43790c40bb53e366/href">https://medium.com/media/6c43bd008582f23d43790c40bb53e366/href</a></iframe><h4>ℹ️ Command Line Prefix Flag</h4><p><strong>CommandLinePrefixFlag</strong> maps build setting value to itself <em>prefixed</em> with a build flag.</p><p>It works just like <strong>CommandLineFlag</strong>. The only difference is that there’s no space between the build flag and the build settings value.</p><p>Good examples include LIBRARY_SEARCH_PATHS and FRAMEWORK_SEARCH_PATHS build settings of list type, where each list entry is mapped to -L&quot;$(value)&quot; or -F&quot;$(value)&quot;, respectively.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f87c0442a64cb4fa6fbc7116e328296d/href">https://medium.com/media/f87c0442a64cb4fa6fbc7116e328296d/href</a></iframe><p>Another similar build setting often used by developers is OTHER_LDFLAGS, aka <em>Other Linker Flags</em>.</p><p>Finally, whenever you see a flag like -fmessage-length=0 it’s most likely mapped using the prefix flag rule.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/39f458b89f1a6d5cef1b3cca12ddd0d9/href">https://medium.com/media/39f458b89f1a6d5cef1b3cca12ddd0d9/href</a></iframe><h4>ℹ️ Additional Linker Flags</h4><p>Certain Swift or Clang compiler build settings map not only to compiler flags, but to linker flags as well.</p><p>Those build settings have an additional <strong>AdditionalLinkerArgs</strong> key-value pair. For example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/264b1c6cdd500707ff9f70a560c9d90a/href">https://medium.com/media/264b1c6cdd500707ff9f70a560c9d90a/href</a></iframe><p>In this example, the -fobjc-arc flag will be added both to the Clang compiler and linker invocations.</p><p>The way mapping works for different build setting types is identical to the handling of <strong>CommandLineArgs</strong>.</p><h3>References</h3><p>As I’ve mentioned earlier, build settings from different xcspecs can reference each other.</p><p>Some build settings have a <strong>DefaultValue</strong> property, which can reference other build settings, e.g. <strong>DefaultValue</strong> = “$(BITCODE_GENERATION_MODE)&quot;;. The default value of some build settings is defined by referencing other build settings:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5f415fec625d29f2c160baf6e0fa2d15/href">https://medium.com/media/5f415fec625d29f2c160baf6e0fa2d15/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/599/1*__6n8xQihimhtj36f2Kz_g.png" /></figure><p>The references can be nested as well. For example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a275129a0fdfe8ab86e5de6a0d6b6fca/href">https://medium.com/media/a275129a0fdfe8ab86e5de6a0d6b6fca/href</a></iframe><p>Here $(DEPLOYMENT_TARGET_SETTING_NAME) will be first resolved into a value like SOME_SETTING_NAME, and then $(SOME_SETTING_NAME) is resolved once again to a final value. This is similar to how build settings are resolved in xcconfigs.</p><p>Build settings reference can be used inside <strong>CommandLineArgs</strong> and all other mapping key-value entries.</p><h3>Conditions</h3><p>Conditions are defined using <strong>Condition</strong> key-value pair and are used to control whether or not a certain build setting is enabled.</p><p>For example, the SWIFT_BITCODE_GENERATION_MODE build setting only makes sense when ENABLE_BITCODE is set to YES:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2687bedb7fe2815127e8d5872c879554/href">https://medium.com/media/2687bedb7fe2815127e8d5872c879554/href</a></iframe><p>Conditions have a C-like syntax, although string values don’t have to be quoted. Boolean operators such as <strong>!</strong>, <strong>&amp;&amp;</strong>, <strong>||</strong>, <strong>==</strong>, and <strong>!=</strong> can be used:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d809883bd25747d2103056b0d915dbc8/href">https://medium.com/media/d809883bd25747d2103056b0d915dbc8/href</a></iframe><h3>Other Properties</h3><p>There are other properties, such as:</p><ul><li><strong>Architectures</strong> — Defines which target architectures the build setting is applicable for.</li><li><strong>AppearsAfter</strong> — Controls the order in which 2 specific build settings appear in Xcode UI.</li><li><strong>FileTypes</strong> — List of applicable file types.</li><li><strong>ConditionFlavors</strong> — Must have something to do with the way conditions are checked.</li><li>There are a few other flags I didn’t have time to fully investigate yet.</li></ul><h3>👩‍🏫 Example</h3><p>Let’s have a look at one build setting example and see how we can apply all the knowledge from this article to figure out which flags it will map to.</p><p>The build setting is CLANG_ENABLE_CODE_COVERAGE, and it enables one very important feature — code coverage.</p><p>CLANG_ENABLE_CODE_COVERAGE is defined in <strong>Clang LLVM 1.0.xcspec</strong> but strangely maps to no flags at all…</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ca9a3ffc8c1da8052d24fa477896b1c7/href">https://medium.com/media/ca9a3ffc8c1da8052d24fa477896b1c7/href</a></iframe><p>However, CLANG_ENABLE_CODE_COVERAGE is referenced by CLANG_COVERAGE_MAPPING.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/03c0943605485a55d084ed028d5df08a/href">https://medium.com/media/03c0943605485a55d084ed028d5df08a/href</a></iframe><p>So now we know the Clang compiler flags are added to the compiler invocation when code coverage is enabled.</p><p>Additionally, there’s another build setting that controls the linker flag:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/97c2c8f122040a3877e6763880897de6/href">https://medium.com/media/97c2c8f122040a3877e6763880897de6/href</a></iframe><p>It also comes with a very detailed comment.</p><p>Finally, CLANG_COVERAGE_MAPPING is also defined in <strong>Swift.xcspec</strong>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e7ab8ef3dc1424a374dc4ea969ddb6b5/href">https://medium.com/media/e7ab8ef3dc1424a374dc4ea969ddb6b5/href</a></iframe><p>Now all the pieces of the puzzle come together and it’s clear how Xcode does the mapping.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*aCozFi55tvX7fgWY4trkag.png" /></figure><p>The only problem is that both CLANG_COVERAGE_MAPPING and CLANG_COVERAGE_MAPPING_LINKER_ARGS are hidden and don’t show up in Xcode UI. So how do those get set if they don’t reference CLANG_ENABLE_CODE_COVERAGE in their default value?</p><p>The code coverage can be enabled by editing the scheme in Xcode UI or by passing -enableCodeCoverage YES to xcodebuild invocation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*aWz8zfclP6fbnEOV1dNvyg.png" /></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/569f14767809e2f1026d88f5ba55d484/href">https://medium.com/media/569f14767809e2f1026d88f5ba55d484/href</a></iframe><p>Xcode will then set both CLANG_COVERAGE_MAPPING and CLANG_COVERAGE_MAPPING_LINKER_ARGS to YES under the hood.</p><h3>👷‍ Application</h3><p>OK then, so it’s more or less clear how build settings are resolved, but what’s the practical application?</p><p>Well, there’s the purely academic application—you get to know how things work, which occasionally will come handy when you have a hard time figuring out various build settings messes.</p><p>While the official <a href="https://help.apple.com/xcode/mac/10.2/#/itcaec37c2a6">Xcode Build Settings</a> reference page is a good resource to use, the <a href="https://github.com/mgrebenets/fastlane-plugin-xcconfig_actions/blob/master/lib/fastlane/plugin/xcconfig_actions/helper/xcspecs/10.2/README.md">extended Xcode Build Settings reference like this one</a> includes information about compiler and linker flags, build settings cross-reference, and more.</p><p>But there are other ways to use this knowledge.</p><p>Let’s say you want to try out an alternative build system like <a href="https://buckbuild.com/">Buck</a> or <a href="https://bazel.build/">Bazel</a>. Those build systems are gaining popularity these days. Buck was created by Facebook while Bazel came from Google. Other companies such as Uber, AirBnB, and Dropbox use Buck; while Lyft is using Bazel to build their mobile apps.</p><p>Let’s further assume that over the years you’ve created and maintained a number of amazing xcconfig files. Those xcconfigs have all the compiler and linker build settings fine-tuned for your use. When moving to tools like Buck, you can’t just bring xcconfigs over. Instead you’d need to translate xcconfigs to compiler and linker flags and then use those with Buck.</p><p>Well, now you have all the knowledge to do so.</p><p>You’d need to start with reading and resolving xcconfigs and then map resolved build settings to build flags. It’s not a straightforward task and may take a while to implement. Luckily for you, there’s a <a href="https://github.com/mgrebenets/fastlane-plugin-xcconfig_actions">Fastlane plugin</a> that does just that.</p><p>Like many unofficial tools, this plugin is reverse engineering the ways Xcode works, so use it at your own risk.</p><p><em>Editor’s Note: </em><a href="https://heartbeat.comet.ml/"><em>Heartbeat</em></a><em> is a contributor-driven online publication and community dedicated to providing premier educational resources for data science, machine learning, and deep learning practitioners. We’re committed to supporting and inspiring developers and engineers from all walks of life.</em></p><p><em>Editorially independent, Heartbeat is sponsored and published by </em><a href="http://comet.ml/?utm_campaign=heartbeat-statement&amp;utm_source=blog&amp;utm_medium=medium"><em>Comet</em></a><em>, an MLOps platform that enables data scientists &amp; ML teams to track, compare, explain, &amp; optimize their experiments. We pay our contributors, and we don’t sell ads.</em></p><p><em>If you’d like to contribute, head on over to our</em><a href="https://heartbeat.fritz.ai/call-for-contributors-october-2018-update-fee7f5b80f3e"><em> call for contributors</em></a><em>. You can also sign up to receive our weekly newsletters (</em><a href="https://www.deeplearningweekly.com/"><em>Deep Learning Weekly</em></a><em> and the </em><a href="https://info.comet.ml/newsletter-signup/"><em>Comet Newsletter</em></a><em>), join us on</em><a href="https://join.slack.com/t/fritz-ai-community/shared_invite/enQtNTY5NDM2MTQwMTgwLWU4ZDEwNTAxYWE2YjIxZDllMTcxMWE4MGFhNDk5Y2QwNTcxYzEyNWZmZWEwMzE4NTFkOWY2NTM0OGQwYjM5Y2U"><em> </em></a><a href="https://join.slack.com/t/cometml/shared_invite/zt-49v4zxxz-qHcTeyrMEzqZc5lQb9hgvw"><em>Slack</em></a><em>, and follow Comet on </em><a href="https://twitter.com/Cometml"><em>Twitter</em></a><em> and </em><a href="https://www.linkedin.com/company/comet-ml/"><em>LinkedIn</em></a><em> for resources, events, and much more that will help you build better ML models, faster.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=733667d01a9a" width="1" height="1" alt=""><hr><p><a href="https://heartbeat.comet.ml/xcode-build-settings-in-depth-733667d01a9a">Xcode Build Settings in Depth</a> was originally published in <a href="https://heartbeat.comet.ml/">Heartbeat</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Working with Fastlane on iOS in an Enterprise Environment]]></title>
            <link>https://heartbeat.comet.ml/working-with-fastlane-on-ios-in-an-enterprise-environment-7057a9a8c2d3?source=rss-241c28b5e61b------2</link>
            <guid isPermaLink="false">https://medium.com/p/7057a9a8c2d3</guid>
            <category><![CDATA[continuous-delivery]]></category>
            <category><![CDATA[heartbeat]]></category>
            <category><![CDATA[ios-app-development]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[fastlane]]></category>
            <dc:creator><![CDATA[Maksym Grebenets]]></dc:creator>
            <pubDate>Tue, 28 May 2019 13:48:28 GMT</pubDate>
            <atom:updated>2021-09-30T15:11:03.059Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FoUEhQmsu_iRPPKbl95yIA.jpeg" /></figure><p>In case you were wondering, no — <a href="https://github.com/fastlane/fastlane">Fastlane</a> doesn’t have an enterprise offering. It’s always been (and I hope will continue to be) an open-source product.</p><p>Fastlane is a collection of tools that help you automate all aspects of mobile development, such as creating new app IDs in the developer portal and managing provisioning profiles; building, testing and packaging apps; and distributing apps to the App Store.</p><p>What this article really centers on is how to use Fastlane while working in an enterprise environment. Specifically those Fastlane actions that need a connection to external URLs.</p><h3>Enterprise Environment</h3><p>If you’re one of the lucky devs who can just “connect to Internet directly”™️, you may as well just skip this article and get back to enjoying Getting Things Done.</p><p>If, my unlucky friend, phrases like <em>company proxy</em> and <em>company firewall</em> sound familiar, then you may find the rest of the article useful.</p><p>So, in very simple terms for non-DevOps engineers like me, working in a big enterprise company usually means that there’s at least one <a href="https://en.wikipedia.org/wiki/Proxy_server">proxy server</a> sitting between you—the developer—and the outside world, aka The Internet. The proxy will make sure to authenticate you securely, just to know who you are; and it will do other important things, like peek inside every single zip archive you download. Why? Because it can…</p><p>To make sure you don’t visit harmful websites like <a href="https://gist.github.com/">GitHub Gists</a> (🤯), there’s also a <a href="https://en.wikipedia.org/wiki/Firewall_(computing)">network firewall</a> thrown into the mix. The firewall will only allow connections to the whitelisted endpoints.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*t3o2vNPkTmARBxqqVn2wGA.png" /></figure><h3>Whitelist</h3><p>If you’re the one tasked with a job of “Uploading ipa file to TestFlight from CI server”, the first thing to do is to request access to Apple endpoints via your company’s proxy and firewall. Mind you, the journey you’re about to embark on will make Homer’s <em>Odyssey</em> look like a pleasant weekend trip.</p><p>Depending on the complexity of your company’s IT infrastructure and level of support, you’d better collect all the information on your side first (and check it twice) before submitting any requests.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*Bd_mTOVpnb_PeRfW_-LaEA.gif" /></figure><p>Now, <a href="https://github.com/fastlane/fastlane/tree/master/spaceship">Spaceship</a> is the core component of Fastlane that enables access both to Apple Developer Center and to App Store Connect. The <a href="https://github.com/fastlane/fastlane/tree/master/spaceship#api-endpoints">list of API endpoints</a> used by Spaceship is a good starting point for the request you’re about to submit.</p><p>I’d even recommend going one step further and requesting whitelist access for all URLs on theapple.com domain. It will make your setup more future-proof, in case Apple adds new APIs or changes existing private APIs, which they do quite often.</p><p>The final list of domains to whitelist should be like this:</p><ul><li>*.apple.com/* - for all kinds of Apple stuff.</li><li>*.devimages.apple.com.edgekey.net/* - this is not strictly required for communicating with the dev portal or App Store Connect, but this is where Fastlane can download iOS Simulator images.</li><li>*.mzstatic.com/* - so that Fastlane can download screenshots of your app from App Store Connect.</li></ul><p>The port to request access is 443 - for all HTTPS traffic.</p><h3>🤖 Service Account</h3><p>Before you go ahead with submitting a new request for your IT infrastructure change, make sure you have a <em>service account</em> as well.</p><p>Unlike normal user accounts, service accounts are not tied to a particular employee. This is very important. When a person leaves a company, their account eventually gets wiped and CI pipelines get broken, so the lesson is: <strong>never use personal accounts for CI setup</strong>. Service accounts don’t ask for a pay raise, don’t chuck a sickie, and they just don’t quit.</p><p>Another good thing about service accounts—they normally don’t have the password expiry policy set. You don’t have to update your CI configuration with a new password every 90 days or so.</p><p>If you don’t have a service account, request one then. Yes, it may mean you have to open yet another request to some other DevOps team, but who said enterprise was easy?</p><h3>Using Fastlane behind the Proxy</h3><p>Fast-forward N weeks, where N is a random large non-negative number, and you have the following at your disposal:</p><ul><li>A service account username and password, and maybe even an email, if you’re super lucky.</li><li>Let’s assume the username is part of the <a href="https://en.wikipedia.org/wiki/Active_Directory#Domain_Services">Active Directory Domain</a> and comes in a au\username form, where au is the domain.</li><li>Let’s also assume the password is just password.</li><li>All requested domains are whitelisted on all levels of intranet.</li><li>You service account can authenticate to the company proxy using username and password and access whitelisted domains.</li><li>Let’s assume the proxy host name is company-proxy and it listens on port 8080 for all incoming connections.</li></ul><h3>Curl Test</h3><p>First off, run a simple curl test to make sure everything works:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8091fb6c4bd889914b6641b62895f298/href">https://medium.com/media/8091fb6c4bd889914b6641b62895f298/href</a></iframe><p>The --proxy-user and --proxy options are self-explanatory, though note the backslash escaping as \\.</p><p>By saying --proxy-anyauth we tell curl to work with any authentication scheme that the proxy supports.</p><p>Most likely, all outgoing network traffic on your dev machine is signed with your company’s own self-signed Root Certificate Authority (CA) certificate, so we use the --insecure option to convince curl to accept this self-signed certificate.</p><p>The self-signed root CA and authentication scheme will play very important roles further on.</p><p>The output of the command should be something like this:</p><blockquote><em>Unauthenticated</em></blockquote><blockquote><em>Request ID: ABCABCBACBACXYZXYZXYZZ37.0.0</em></blockquote><p>This is good. It means the request went all the way to appstoreconnect.apple.com.</p><h3>🔏 Root CA and SSL Certificates</h3><p>Just like with curl, a self-signed root CA certificate won’t be accepted by Fastlane either. While running Fastlane, you won&#39;t have any option like the curl&#39;s --insecure flag. Instead you need to add your internal root CA certificate to <a href="https://github.com/rubygems/rubygems">rubygems</a> path, because Fastlane is a Ruby gem:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6e7add51c5255547f844910b145682d9/href">https://medium.com/media/6e7add51c5255547f844910b145682d9/href</a></iframe><p>You may have to use sudo if you’re using the system Ruby version, though I&#39;d recommend using <a href="http://rvm.io/">RVM</a> or <a href="https://github.com/rbenv/rbenv">rbenv</a> to manage your rubies.</p><p>If you are using RVM, you’ll need to run the following as well:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/eb42199e62a014229bf8126553ba7a28/href">https://medium.com/media/eb42199e62a014229bf8126553ba7a28/href</a></iframe><p>Now you can use <a href="https://gist.github.com/mgrebenets/ebab9213959319779807ce39bd0bed56">this Ruby script</a> to test the SSL connection.</p><p>E.g. just run it directly like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/148d2767ce206502a1e06a4518b6cf24/href">https://medium.com/media/148d2767ce206502a1e06a4518b6cf24/href</a></iframe><h3>💲 Environment Variables</h3><p>The next thing you’ll notice is that Fastlane doesn’t have any option for passing proxy information. Luckily, Fastlane is being a good citizen and respects all the /http[s]_proxy/i environment variables, namely:</p><ul><li>HTTP_PROXY</li><li>HTTPS_PROXY</li><li>http_proxy</li><li>https_proxy</li></ul><p>Remember this <em>“Gang of Four”—</em>nothing else will cause you as much pain as this bunch!</p><p>Somewhere in your ~/.bash_profile or other appropriate shell profile, set these environment variables—for example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d8e0de838ca46416e55279b9d561711c/href">https://medium.com/media/d8e0de838ca46416e55279b9d561711c/href</a></iframe><p>Note that proxy URL is set in the following format:</p><blockquote><em>http://</em><em>&lt;domain&gt;\</em><em>&lt;username&gt;:</em><em>&lt;password&gt;@</em><em>host:</em><em>port</em></blockquote><p>Without the username and password, you wouldn’t be able to authenticate with your proxy.</p><p>Now give it a try. Run the simple download_screenshots <a href="https://docs.fastlane.tools/actions/deliver/">deliver</a> action:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0b24241a5dc6786a2e3706dd2d092850/href">https://medium.com/media/0b24241a5dc6786a2e3706dd2d092850/href</a></iframe><p>Answer all the prompts and see if it worked. I bet it didn’t!</p><p>Long story short, Fastlane is using the <a href="https://github.com/lostisland/faraday">faraday</a> HTTP client Ruby library, which uses <a href="https://ruby-doc.org/stdlib-2.5.1/libdoc/uri/rdoc/Kernel.html">Kernel.URI</a> method, which can’t handle a backslash \ in the username.</p><p>The solution is to use <a href="https://en.wikipedia.org/wiki/Percent-encoding">percent-encoding</a> for \, i.e. %5c:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/719b84c0c984094da8384b35398030ec/href">https://medium.com/media/719b84c0c984094da8384b35398030ec/href</a></iframe><p>You should be able to download screenshots now, though that’s just a first, tiny step towards the final goal. Well, actually, you can do a lot of things now, such as registering devices, managing App IDs and provisioning profiles, and more. But I’d assume you also want to upload a new ipa to <a href="https://developer.apple.com/testflight/">TestFlight</a>, so meet The Transporter then...</p><h3>🚎 Transporter</h3><p><a href="https://itunespartner.apple.com/en/movies/faq/Transporter_Getting%20Set%20Up">Transporter</a>, aka <a href="https://help.apple.com/itc/transporteruserguide/#/apdAbeb95d60">iTMSTransporter</a>, is a Java-based command-line tool used to “upload things to the App Store”, including app metadata and new ipa builds.</p><h4>Installing Transporter</h4><p>So how do you get a copy?</p><p>Well…<a href="https://help.apple.com/itc/transporteruserguide/#/apdA3ae5a8b0?sub=apdA687d545d">according to Apple</a>, you first login to your developer account and then click the download link <a href="https://itunesconnect.apple.com/WebObjects/iTunesConnect.woa/ra/resources/download/Transporter__OSX/bin/">like this one</a>.</p><p>But wait! You can’t download it unless you have an <em>Admin</em> or <em>Technical</em> role.</p><p>I mean, “Why, Apple?! Why?!” 😱</p><p>Transporter is available as part of the Xcode installation anyways…why make things so hard?</p><p>Why not make it available at <a href="https://developer.apple.com/download/more/">Apple Developers Downloads</a> at the very least?</p><p>Anyways, if you choose to use the copy bundled with Xcode, then you can find it at:</p><blockquote><em>/Applications/Xcode.app/Contents/Applications/Application Loader.app/Contents/itms</em></blockquote><p>Just grab the whole directory.</p><p>If you installed it via the Mac OS package, then get it at:</p><blockquote><em>/usr/local/itms</em></blockquote><h4>Patching Transporter</h4><p>If you’re wondering: “Why do I need a copy of Transporter? Can’t Fastlane just use it as is?”</p><p>Good question. You don’t need a copy of Transporter. You can use the one available inside Xcode or in your /usr/local, but you would have to patch it before using it anyways.</p><p>Given that Transporter is a Java-based <em>command-line</em> app, it has the same issue as curl or any Ruby gem would have with your company&#39;s internal <em>self-signed</em> root CA certificate - it won&#39;t trust it. Apple didn&#39;t bother to add a command line or configuration option of any kind to Transporter to trust self-signed certificates.</p><p>Thankfully, Transporter isn’t <em>just</em> a Java-based app, it comes with its own version of Java runtime bundled up inside itms/java. Try running itms/java/bin/java -version to see more info.</p><p>Now, the itms/java folder also contains lib/security/cacerts file, which is a <a href="https://www.ibm.com/support/knowledgecenter/en/SSYKE2_8.0.0/com.ibm.java.security.component.80.doc/security-component/keytoolDocs/cacertsfile.html">keystore with CA certificates</a>. So you need to add your company certificate to cacerts, and itms/java/bin/keytool is just the right tool to do that.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/65d3a14d8d71820aea8f8050c1765b93/href">https://medium.com/media/65d3a14d8d71820aea8f8050c1765b93/href</a></iframe><p>Here ${ROOT_CA_PATH} is the path to the certificate file, which you can locate directly or find in the OS X keychain</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bd00466db93eb0081f648c8bed0757ab/href">https://medium.com/media/bd00466db93eb0081f648c8bed0757ab/href</a></iframe><p>Now you’ve convinced Transporter to trust your root CA, but you’re not there yet, because you also need to teach Transporter some respect towards your new and shiny proxy, which is where trouble awaits for you…again.</p><h4>Transporter and Proxy</h4><p>Now that we know that Transporter is a Java-based app, we look closer inside theitms folder and find itms/java/lib/net.properties - a Java properties file used to configure the <a href="https://en.wikipedia.org/wiki/Java_virtual_machine">JVM</a> that Transporter runs in.</p><p>The group of proxy variables is exactly what we need:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b73d751de8032dbd67ff11f82308f828/href">https://medium.com/media/b73d751de8032dbd67ff11f82308f828/href</a></iframe><p>You don’t even have to modify the net.properties file. Fastlane reads the value of the special DELIVER_ITMSTRANSPORTER_ADDITIONAL_UPLOAD_PARAMETERS environment variable and uses it to configure Transporter. For example, in your bash script add the following export statement:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3f7d4ef93249a8d246b3e8e2e14d544c/href">https://medium.com/media/3f7d4ef93249a8d246b3e8e2e14d544c/href</a></iframe><p>But will it work? By now you’ve been there enough times to know the answer — “No”. 😜</p><p>Proxy configuration needs to include the username and password for <a href="https://en.wikipedia.org/wiki/Basic_access_authentication">Basic authentication</a>.</p><p>You can try to add au%5cusername:password@company-proxy as the host value, but it won’t work.</p><p>Removing the jdk.http.auth.tunneling.disabledSchemes=Basic limitation from net.properties won&#39;t help either.</p><p>While trying to Google you may find mention of these properties:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e0bbde5fc55f64fdb36f5abce551e080/href">https://medium.com/media/e0bbde5fc55f64fdb36f5abce551e080/href</a></iframe><p>Those are part of <a href="https://hc.apache.org/">Apache HTTP Client</a> JVM options, though, and are not part of <a href="https://docs.oracle.com/javase/8/docs/technotes/guides/net/proxies.html">Oracle’s default network properties</a>. Also, Transporter doesn’t respect them.</p><p>🚧 Feels like another roadblock.</p><h4>Another Proxy</h4><p>The solution this time is to create yet another proxy (as if there weren’t enough problems with them already!).</p><p>This “man in the middle” <em>local</em> proxy will take care of passing authentication information to your company proxy. The credentials no longer have to be hard-coded in a proxy URL. This way, the command line tools like Transporter can use a <em>local</em> proxy for network connections, and a local proxy doesn’t require any type of authentication.</p><p>There are a number of proxy tools available for Mac OS. In this example, I’ll be using <a href="http://cntlm.sourceforge.net/">CNTLM</a>.</p><p>I’ll briefly mention that I also tried <a href="https://tinyproxy.github.io/">TinyProxy</a> and <a href="https://github.com/rofl0r/proxychains-ng">ProxyChains-NG</a>. TinyProxy just wouldn’t let me use \ or %5c in the configuration file, while with ProxyChains-NG I eventually just gave up after a number of failed attempts to make it work.</p><p>So, <em>CNTLM—</em>a fast proxy for <a href="https://en.wikipedia.org/wiki/NT_LAN_Manager">NTLM</a> authentication implemented in C.</p><p>Easy to install.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/56dcb9f3ea46749f0a7d0d0d18b0c8bd/href">https://medium.com/media/56dcb9f3ea46749f0a7d0d0d18b0c8bd/href</a></iframe><p>Also easy to configure. Just put the following in cntlm.conf:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d0b5f8676aba6a3c833c34083cfa02b1/href">https://medium.com/media/d0b5f8676aba6a3c833c34083cfa02b1/href</a></iframe><p>Now run this command to generate password hashes:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/686eebd67432951407a1474aa9719e87/href">https://medium.com/media/686eebd67432951407a1474aa9719e87/href</a></iframe><p>Type in your account password, copy hashes, and append them to cntlm.conf:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5b5bd675367032a170f71e2c9cda6eaf/href">https://medium.com/media/5b5bd675367032a170f71e2c9cda6eaf/href</a></iframe><p>Then run it:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a0f1668311d8336ba52eccecb6f9bed3/href">https://medium.com/media/a0f1668311d8336ba52eccecb6f9bed3/href</a></iframe><p>Finally, configure proxy environment variables:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f72c65756a4676a62410a0627a93ee3e/href">https://medium.com/media/f72c65756a4676a62410a0627a93ee3e/href</a></iframe><p>Give it a go!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8d0ba6187c9ae49869175ed3664cbdee/href">https://medium.com/media/8d0ba6187c9ae49869175ed3664cbdee/href</a></iframe><p>What do you mean “It doesn’t work!?” Something about the proxy returning an invalid challenge?</p><p>Well, that just means you company proxy doesn’t support NTLM authentication and only supports Basic authentication. That needs to be changed by requesting IT support again, I’m afraid.</p><p>Now the curl test should finally work, and your configuration now looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*K5VMNxNCOcvIFhD-Vx5Mpw.png" /></figure><h3>Upload ipa</h3><p>You’re all set now to run one of the most common and recurring tasks in the <em>Continuous Delivery</em> process for an iOS app — upload a new build to <a href="https://developer.apple.com/testflight/">TestFlight</a>.</p><p>Start by setting up the basic lane using <a href="https://docs.fastlane.tools/actions/pilot/">pilot</a> action.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d1e9fe70848679d60e7dbe59ff6b807d/href">https://medium.com/media/d1e9fe70848679d60e7dbe59ff6b807d/href</a></iframe><p>To skip the password prompt, set the FASTLANE_PASSWORD environment variable. It could be a secret parameter in your CI server configuration, or you could save it in your OS X keychain as AppleID.Password and read like so:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/964dd5e8276f4e1d86166d1af349c55c/href">https://medium.com/media/964dd5e8276f4e1d86166d1af349c55c/href</a></iframe><p>It’s important not to confuse the Apple ID used as <em>username</em> with the app’s Apple ID. The latter is the unique identifier the app is assigned in the App Store—that’s why it’s often referred to as <em>Adam</em> or <em>Adam/Apple/App</em> ID. You can find out the Adam ID of the app by navigating to it in App Store Connect and grabbing the last part of the URL.</p><p>Then again, the app_identifier parameter is actually the Bundle Identifier stored under CFBundleIdentifier in the application&#39;s Info.plist.</p><p>In this example, we don’t want to submit a new build for review yet, and we don’t want to wait for processing either, thus both skip_submission and skip_waiting_for_build_processing are set to true.</p><p>Note that with most of Fastlane actions, each action parameter is shadowed by an environment variable. For example, instead of setting the ipa parameter, you can set the PILOT_IPA environment variable. To see a full list of parameters for an action, run bundle exec fastlane action &lt;action_name&gt;, for example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/75bc1f559d8ef689e02b85d75a37746b/href">https://medium.com/media/75bc1f559d8ef689e02b85d75a37746b/href</a></iframe><p>Try to run the lane now—after setting up all the environment variables, of course:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0ba7b323859f71c74072f20d2e50bbfa/href">https://medium.com/media/0ba7b323859f71c74072f20d2e50bbfa/href</a></iframe><h4>🤝 Fastlane Session</h4><p>If you plan to use similar action on CI server, then <a href="https://en.wikipedia.org/wiki/Multi-factor_authentication">Two-factor authentication (2FA)</a> enforced by Apple will become an issue. To avoid 2FA on CI servers, you need to set the FASTLANE_SESSION environment variable as described <a href="https://github.com/fastlane/fastlane/tree/master/spaceship#support-for-ci-machines">here</a>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5c075e95b2cff244d46b3723ebaeb4cd/href">https://medium.com/media/5c075e95b2cff244d46b3723ebaeb4cd/href</a></iframe><p>Whenever using 2FA, the browser will prompt you, asking you if you want to trust this particular browser. Doing so will generate a long-living session token (30 days), which will then be picked by Fastlane’s spaceauth command. This way you’ll only have to update your CI server configuration once every 30 days. There may be <a href="https://github.com/fastlane/fastlane/issues/14305">some ways</a> to keep the session token up to date by running scheduled CI jobs that perform Spaceship login, though I haven&#39;t tested that approach yet.</p><h3>Summary</h3><p>By the end of this journey, you should be able to upload new iOS app builds to TestFlight from the enterprise network, e.g. from a CI build agent.</p><p>It’s amazing how such a basic task requires so much extra effort when dealing with all the overhead inherent to most large companies.</p><p>I hope it was worth the effort, though. Once it’s all set up and runs smoothly, there are so many other aspects of iOS development that can be automated using tools like Fastlane.</p><p><em>Editor’s Note: </em><a href="https://heartbeat.comet.ml/"><em>Heartbeat</em></a><em> is a contributor-driven online publication and community dedicated to providing premier educational resources for data science, machine learning, and deep learning practitioners. We’re committed to supporting and inspiring developers and engineers from all walks of life.</em></p><p><em>Editorially independent, Heartbeat is sponsored and published by </em><a href="http://comet.ml/?utm_campaign=heartbeat-statement&amp;utm_source=blog&amp;utm_medium=medium"><em>Comet</em></a><em>, an MLOps platform that enables data scientists &amp; ML teams to track, compare, explain, &amp; optimize their experiments. We pay our contributors, and we don’t sell ads.</em></p><p><em>If you’d like to contribute, head on over to our</em><a href="https://heartbeat.fritz.ai/call-for-contributors-october-2018-update-fee7f5b80f3e"><em> call for contributors</em></a><em>. You can also sign up to receive our weekly newsletters (</em><a href="https://www.deeplearningweekly.com/"><em>Deep Learning Weekly</em></a><em> and the </em><a href="https://info.comet.ml/newsletter-signup/"><em>Comet Newsletter</em></a><em>), join us on</em><a href="https://join.slack.com/t/fritz-ai-community/shared_invite/enQtNTY5NDM2MTQwMTgwLWU4ZDEwNTAxYWE2YjIxZDllMTcxMWE4MGFhNDk5Y2QwNTcxYzEyNWZmZWEwMzE4NTFkOWY2NTM0OGQwYjM5Y2U"><em> </em></a><a href="https://join.slack.com/t/cometml/shared_invite/zt-49v4zxxz-qHcTeyrMEzqZc5lQb9hgvw"><em>Slack</em></a><em>, and follow Comet on </em><a href="https://twitter.com/Cometml"><em>Twitter</em></a><em> and </em><a href="https://www.linkedin.com/company/comet-ml/"><em>LinkedIn</em></a><em> for resources, events, and much more that will help you build better ML models, faster.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7057a9a8c2d3" width="1" height="1" alt=""><hr><p><a href="https://heartbeat.comet.ml/working-with-fastlane-on-ios-in-an-enterprise-environment-7057a9a8c2d3">Working with Fastlane on iOS in an Enterprise Environment</a> was originally published in <a href="https://heartbeat.comet.ml/">Heartbeat</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Provisioning Profiles, Sigh…]]></title>
            <link>https://medium.com/@maksymgrebenets/provisioning-profiles-sigh-20637b1b51b0?source=rss-241c28b5e61b------2</link>
            <guid isPermaLink="false">https://medium.com/p/20637b1b51b0</guid>
            <category><![CDATA[ci]]></category>
            <category><![CDATA[provisioning]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[fastlane]]></category>
            <category><![CDATA[sigh]]></category>
            <dc:creator><![CDATA[Maksym Grebenets]]></dc:creator>
            <pubDate>Sun, 21 Apr 2019 13:06:31 GMT</pubDate>
            <atom:updated>2019-04-21T13:06:31.955Z</atom:updated>
            <content:encoded><![CDATA[<p>In this article you will find out why iOS provisioning profiles are a nightmare and if there is a way to automate provisioning profiles management.</p><h3>Provisioning Profiles</h3><p>Anyone who calls themselves an iOS Developer, knows what this is all about. You have to generate profiles, make sure they use proper certificates and are configured for the right app identifier. Should you renew a certificate or change the app’s entitlements, you now have to regenerate all related provisioning profiles, then make sure you install new profiles everywhere you need them. If it wasn’t difficult enough already, there are 2 types of provisioning profiles: Development and Distribution; and if that wasn’t enough, Distribution profiles come in two more flavors: App Store and AdHoc.</p><p>But that’s not the end of the nightmare either! What if you are a member of more than one iOS developer program? For example, you are part of both App Store and Enterprise programs, or you are a freelancer and happen to be a member of a dozen teams. Did you use different Apple IDs for different teams? Doesn’t make it any easier at all. I’m sure many of you know what it feels like.</p><p>Finally, things go wild if you have to manage that provisioning zoo for Continuous Integration setup. Just one build box is painful enough to keep up to date, I’m not even mentioning multiple build agents yet…</p><p>Of course I’m over pessimistic, but that’s on purpose. One could say that Xcode can manage your accounts and profiles. That’s true but Xcode is known to do a very sloppy job in that regards, and in no way Xcode’s provisioning profile management can be <em>automated</em>.</p><p>Overall, managing provisioning profiles involves a lot of <strong>manual</strong> interactions with web browser. What if there was a tool that would let you manage profiles automatically, via command line and scripts? Well, it so happens that there is one now! Meet <a href="https://docs.fastlane.tools/actions/sigh/">Sigh</a> a part of <a href="https://docs.fastlane.tools/">Fastlane</a> tools. Fastlane is a collection of tools that cover all aspects of iOS development, Sigh in particular is designed to help you with managing provisioning profiles and certificates. You can manage profiles installed locally as well as create, renew, update, regenerate and delete profiles from developer portal.</p><p>There are so many uses for the tool, but in this article I’ll describe the most basic one, that is downloading an existing provisioning profile. As a bonus, you will see how to parse provisioning profiles and get all the details from it, especially profile UUID and signing identity of the related certificate.</p><h3>Download Profile</h3><p>Time to fix all the assumptions before going forward.</p><ul><li>You have an account with Enterprise development program and that account has <strong>Admin</strong> level of access.</li><li>You have a Distribution provisioning profile created for Wildcard App ID (*).</li></ul><p>That will do it. First thing to do is to install Fastlane Ruby gem.</p><blockquote><em>gem install fastlane</em></blockquote><p>Time to get a profile now.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8e86f0ae41b393a48c8c08f0eae5256f/href">https://medium.com/media/8e86f0ae41b393a48c8c08f0eae5256f/href</a></iframe><p>So what’s going on here?</p><ul><li>--username is the Apple ID you used to join the developer program.</li><li>--team_id is the 10 character ID of the team. This is mandatory if the Apple ID is part of multiple teams (aka programs). In general, it&#39;s just a good rule of thumb to be as explicit as possible.</li><li>--provisioning_name is the name of provisioning profile. That&#39;s the very same name you see when you login to developer portal, not UUID or any other kind of identifier.</li><li>--app_identifier is the App identifier in the provisioning profile. The same thing you use as Bundle ID most of the time. In this example the profile is a wildcard profile, so app identifier is just &quot;*&quot; (wildcard). Be cautious! Those quotes around * <em>really matter</em>!</li><li>--filename tell sigh where to save a copy of the provisioning profile file. By default it will be installed in ~/Library/MobileDevice/Provisioning Profiles/. If you don&#39;t want the profile to be installed, add --skip_install option to the command.</li></ul><p>The very first time you use a given combination of Apple ID and Team ID you will be asked for a password. Fastlane will safely store your password in keychain and you will not have to type it in anymore (well, until the moment you change it, of course). So for CI setup you will have to use Sigh (or any other tool from Fastlane) at least once for each developer account, to have the password stored, and from that moment on all CI scripts will be able to work without user intervention required.</p><p>The above is not the case with the introduction of 2FA. You would have to configure FASTLANE_SESSION as described <a href="https://github.com/fastlane/fastlane/tree/master/spaceship#support-for-ci-machines">here</a>.</p><blockquote><em>A side note in regards to Team ID. For some reason the real ID is very obscure and doesn’t match the one you would see in Member Center. Luckily, you can run Sigh with wrong Team ID and it will output the list of all correct IDs.</em></blockquote><h3>Parse Profile</h3><p>Another step towards total automation is to include profile download as part of CI job. Usually all CI jobs expect latest provisioning profiles to be installed on the box. CI is either using UUID explicitly, expects profile to have a specific file name or delegates profile lookup task to Xcode. With Sigh you can simplify this bit immensely. Each CI job will make sure the latest profile is downloaded and installed, and named the way it should be named.</p><p>Additionally, you may want to extract information from the profile, such as the profile UUID and the name of the signing identity. Why would you do that? Well, you can build the very same project using various provisioning profiles and signing identities, this is the way you can get different builds for Enterprise, AdHoc and App Store distribution, with different bundle IDs, etc. Usually build scripts are configured to specify the provisioning profile specifier and signing identity explicitly via xcodebuild&#39;s PROVISIONING_PROFILE_SPECIFIER and CODE_SIGN_IDENTITY build settings. This is how you can overwrite default build settings in Xcode project.</p><p>While specifying signing identity explicitly is important, specifying profile specifier is something that should be done with a lot of caution. The reason is Today Widget and WatchKit extensions. These extensions are separate targets in Xcode project and by default extension targets are dependencies for main app target. It means when you build a main target, all extensions are built as well. Nothing bad so far. The problem is that each of those extensions has its own bundle ID and, as you would expect, a separate provisioning profile. Now, if you explicitly specify profile specifier when building main app target, this build setting will overwrite build settings for all dependent targets as well. Naturally, the build will fail when trying to build Today Widget extension with bundle ID and entitlements from main app target provisioning profile.</p><p>So what do you do? Well, previously I’d say to trust Xcode, but these days I’d recommend to use custom build settings instead. E.g. define build settings in this way:</p><ul><li>For main app: PROVISIONING_PROFILE_SPECIFIER = MAIN_APP_PROVISIONING_PROFILE_SPECIFIER</li><li>For extension: PROVISIONING_PROFILE_SPECIFIER = EXTENSION_PROVISIONING_PROFILE_SPECIFIER</li></ul><p>Now you can control individual provisioning profile specifiers for each target, or even put them in a xcconfig file and use that file to override build settings.</p><p>In any case, it’ still helpful to be able to parse the provisioning profile, e.g. if you want to have some post build verification to make sure the correct profile has been used for the build.</p><p>Start with dropping all the digital signature stuff and getting just the profile XML (plist) part.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/910ee457c5f8f38d1259944caec86dc9/href">https://medium.com/media/910ee457c5f8f38d1259944caec86dc9/href</a></iframe><p>I can’t tell you what <em>exactly</em> all arguments of security command mean, you may find better answers in the <a href="http://www.objc.io/issue-17/inside-code-signing.html">Inside Code Signing</a> article, where I learned about this command and other commands you will see in the rest of this post. The result is saved to profile.plist.</p><p>Now you have a plist with all the data. Let’s get UUID first. The tool that can work with plists is /usr/libexec/PlistBuddy.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/714b1360dd03d26d3dcb3ce1ce1f82dd/href">https://medium.com/media/714b1360dd03d26d3dcb3ce1ce1f82dd/href</a></iframe><p>This doesn’t need much explanation. If you run this command you will get the UUID of the profile as an output.</p><p>Next is signing identity, which is a bit more difficult. Signing identity name is not stored as plain text in the profile, however, it is part of the certificate. The profile stores all associated certificates in DeveloperCertificates key-value pair. That&#39;s right, there can be more than one certificate associated with one provisioning profile. It&#39;s hard to figure out which of the certificates to pick automatically. The assumption is that all of those certificates are valid and any of them can be used to sign the app bundle. If that&#39;s not the case, you need to tidy up things in your developer portal. Sigh can actually help you to do that from command line as well. Anyway, let&#39;s assume the first certificate is the one you need, time to get it.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/810dc0dc30e63bb54c5b9a969fbc1d05/href">https://medium.com/media/810dc0dc30e63bb54c5b9a969fbc1d05/href</a></iframe><p>Oops, looks rather ugly. Seems like PlistBuddy is doing some base 64 decoding under the hood. You can feed this output to base64 utility to encode it back and see the same data as in plist, but do you really need to? Instead, just feed this unreadable data directly to openssl and ask for the subject field.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5ee0c27a12135b5761ffa6877fc9d675/href">https://medium.com/media/5ee0c27a12135b5761ffa6877fc9d675/href</a></iframe><p>The output is something like</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1a7764ff63da8c7afc90f207d68f9da0/href">https://medium.com/media/1a7764ff63da8c7afc90f207d68f9da0/href</a></iframe><p>You only need to get Common Name (CN) part. At the moment I didn’t find anything better than using sed as you can see below. It extracts the string between &quot;/CN=&quot; and &quot;/OU=&quot;. I don&#39;t know in which flavors certificates come, could be this regex needs adjustments in case the order of fields in subject is different.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/572b5f11819e366d1284adfa811ffcfe/href">https://medium.com/media/572b5f11819e366d1284adfa811ffcfe/href</a></iframe><p>Alternatively, you can use this regex</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/030d35d9232f5c1e5a32953bbbb109ca/href">https://medium.com/media/030d35d9232f5c1e5a32953bbbb109ca/href</a></iframe><p>It works with the assumption there are no / s in the common name. I think the message here is</p><blockquote><em>Be reasonable and don’t use </em><em>=s or </em><em>/ s when naming new certificate.</em></blockquote><h3>Summary</h3><p>So now you have a way to download and parse provisioning profile. Automatically, in one go. With little bit of work you can come up with a proper shell script for the task.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4e6f92b9c1f6b7bb84f5b8ccbd6edb49/href">https://medium.com/media/4e6f92b9c1f6b7bb84f5b8ccbd6edb49/href</a></iframe><p>Wit a bit more work you can refactor it to take username, team id and other parameters as input arguments.</p><p><em>Originally published at </em><a href="http://nsbogan.com/mobile%20ci/2015/05/15/provisioning-profiles-sigh"><em>http://nsbogan.com</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=20637b1b51b0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Xcode Build Phases and Environment]]></title>
            <link>https://medium.com/@maksymgrebenets/xcode-build-phases-and-environment-7851e376eb03?source=rss-241c28b5e61b------2</link>
            <guid isPermaLink="false">https://medium.com/p/7851e376eb03</guid>
            <category><![CDATA[bash]]></category>
            <category><![CDATA[xcode]]></category>
            <category><![CDATA[mobile]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[shell]]></category>
            <dc:creator><![CDATA[Maksym Grebenets]]></dc:creator>
            <pubDate>Thu, 04 Apr 2019 00:00:00 GMT</pubDate>
            <atom:updated>2019-04-07T03:03:49.092Z</atom:updated>
            <content:encoded><![CDATA[<p>How to teach Xcode to respect the Environment.</p><p>If you’ve ever done iOS development you’ve surely used <a href="https://help.apple.com/xcode/mac/10.2/#/dev50bab713d">Xcode Build Phases</a>. One of the tasks that a build phase can perform is <a href="https://help.apple.com/xcode/mac/10.2/#/devc8c930575">running a shell script</a>, and that’s where you can face one peculiar problem…</p><p>The problem is that shell scripts ran in Xcode build phases <strong>do not</strong> source the user shell profile.</p><p>Try to put the following in your shell profiles:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a8c6573065dba8990df284497edc0d4a/href">https://medium.com/media/a8c6573065dba8990df284497edc0d4a/href</a></iframe><p>Then run the build phase by building Xcode project.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*sXsZZ05GSAhZL0g6.png" /></figure><p>None of the messages from shell profiles will show up.</p><p>The reason is that a shell ran from Xcode build phase is <a href="https://www.vanimpe.eu/2014/01/18/different-shell-types-interactive-non-interactive-login/">non-interactive shell</a>.</p><p>It is actually not a bug, but an expected behavior, which may confuse you a bit though.</p><h3>The Example</h3><p>As an example, let’s say you’ve got a Ruby script which you want to run as part of Xcode build phase.</p><p>The script happens to be using some 2.5.x Ruby language features, which are not available in 2.3.x.</p><p>You are also using <a href="http://rvm.io/">RVM</a> to install and use version 2.5.x.</p><p>Now add ruby --version to the build phase, build the project and check the output. On Mojave OS X the output will be 2.3.7, which is the system Ruby version.</p><p>None of the RVM environment usually defined in shell profile got loaded into script.</p><p>One solution to this problem is to use <a href="https://www.vanimpe.eu/2014/01/18/different-shell-types-interactive-non-interactive-login/">login shell</a>.</p><p>It’s as simple as adding -l to the “Shell” parameter of the build phase, for example /bin/sh -l. However, it will <strong>not</strong> work for <a href="https://en.wikipedia.org/wiki/Bourne_shell">Bourne shell (sh)</a>.</p><p>But then it <strong>will</strong> work for the <a href="https://en.wikipedia.org/wiki/Bash_(Unix_shell)">Bourne-again shell (bash)</a>, so you could change “Shell” parameter to say /bin/bash -l.</p><p>You will see this in the logs now:</p><blockquote><em>Loading ~/.bash_profile</em></blockquote><p>That works, but is not ideal. If you have a lot of developers on the team, they may have all kinds of things happening in their ~/.bash_profile which you don’t necessarily want to run in Xcode build phase. All you want is only load RVM environment, nothing else.</p><p>Other developers may as well use <a href="https://en.wikipedia.org/wiki/Z_shell">zsh</a> or some other kind of shell.</p><h3>Build Phase Scripts</h3><p>Another way to tackle this problem is to source just the things that matter as part of each build phase script.</p><p>For example, to use RVM for loading Ruby version in non-interactive shell, you could use this code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dae9cc5cdd7296195f915f3ef1e1f925/href">https://medium.com/media/dae9cc5cdd7296195f915f3ef1e1f925/href</a></iframe><p>Here we first add rvm command to the PATH. Then we read Ruby version used by the project from .ruby-version file, which is one of the common setups. Finally we get a shell script containing all the environment variables using rvm &quot;${RUBY_VERSION}&quot; do rvm env --path and source (load) that script into current shell session.</p><p>Now you can put this code to a <a href="https://github.com/bkeepers/dotenv">dotenv</a> file called .env.ruby, for example. Each build phase shell script that requires Ruby can now add source .env.ruby line.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*RD5_RwzVu-6HMYHc.png" /></figure><p>While this approach may look like it requires more work, it is still a better one for a couple of reasons:</p><ul><li>You only load the things you need to run your script, not all the things from user’s shell profile</li><li>The Ruby version is selected based on the project’s .ruby-version file, while with login shell approach the default Ruby version set for RVM globally is loaded and you’d have to run extra code to switch to correct version</li></ul><h3>Tips</h3><p>A few tips to using shell script build phases.</p><h4>Script Errors</h4><p>Add -e flag for build phase to fail if shell script returns error code other that 0, e.g. /bin/sh -e.</p><h4>Managing Build Phase Scripts</h4><p>Use the following convention to manage you build phase scripts:</p><blockquote><em>There must be one shell script per build phase.</em></blockquote><p>For example, put all your build phase scripts in a directory named build-phases, e.g. build-phases/codecheck.sh, build-phases/codegen.sh, etc.</p><p>In Xcode build phase UI instead of having an inline script, you have this now:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*-jwCoZSBstii6CY2.png" /></figure><p>You can even move the Ruby environment loading line inside the build phase script to keep it down to a one-liner.</p><p>It’s a much better approach for a number of reasons:</p><ul><li>If you modify the build phase script, it’s much easier to review it in pull request as a change of .sh file, compared to when you have to review it as part of pbxproj UTF-8 ASCII plist file changes with all the newline and other escape sequences added into the mix.</li><li>You can reuse build phase scripts outside of Xcode, e.g. if you try to use alternative build system like <a href="https://buckbuild.com/">Buck</a>.</li></ul><p><em>Originally published at </em><a href="http://nsbogan.com/xcode/2019/04/04/xcode-build-phases-and-environment"><em>nsbogan.com</em></a><em> on April 4, 2019.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7851e376eb03" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Swift Refactoring — Summary]]></title>
            <link>https://medium.com/@maksymgrebenets/swift-refactoring-summary-faf9bb2d138?source=rss-241c28b5e61b------2</link>
            <guid isPermaLink="false">https://medium.com/p/faf9bb2d138</guid>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Maksym Grebenets]]></dc:creator>
            <pubDate>Wed, 06 Feb 2019 00:00:00 GMT</pubDate>
            <atom:updated>2019-03-04T10:22:54.081Z</atom:updated>
            <content:encoded><![CDATA[<h3>Swift Refactoring — Summary</h3><p>A summary for the <a href="https://medium.com/@maksymgrebenets/swift-refactoring-intro-8df528906840">series of articles</a> about implementing <a href="https://swift.org/blog/swift-local-refactoring/">Swift Local Refactoring</a> action.</p><p>If you came here after going through previous 6 articles, you may have a few questions to ask:</p><p><em>Was it worth it?</em></p><p>Sure it was.</p><p>I learned the basics of working with Swift code base. How to <a href="https://medium.com/@maksymgrebenets/swift-refactoring-setup-f5c8af8fccff">build it</a>, how to <a href="https://medium.com/@maksymgrebenets/swift-refactoring-tests-c30e3694dc49">write tests</a> and <a href="https://medium.com/@maksymgrebenets/swift-refactoring-debugging-a682b972899a">debug</a> the code.</p><p>This whole exercise took me one tiny step closer to understanding the rest of the Swift project.</p><p><em>Would you do it again?</em></p><p>The answer is “Yes” and “No”.</p><p>No, I would probably not try to implement another refactoring action via swift-refactor. Instead, I’d look into using <a href="https://github.com/apple/swift/tree/master/lib/Syntax">libSyntax</a> library.</p><p>Yes, if I had more time to spare I’d look at one of the many <a href="https://bugs.swift.org/browse/TF-130?jql=labels%20%3D%20StarterBug">Swift starter tasks</a>.</p><p><em>Originally published at </em><a href="http://nsbogan.com/swift/2019/02/06/swift-refactoring-summary"><em>nsbogan.com</em></a><em> on February 6, 2019.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=faf9bb2d138" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Swift Refactoring — Debugging]]></title>
            <link>https://medium.com/@maksymgrebenets/swift-refactoring-debugging-a682b972899a?source=rss-241c28b5e61b------2</link>
            <guid isPermaLink="false">https://medium.com/p/a682b972899a</guid>
            <category><![CDATA[ios]]></category>
            <dc:creator><![CDATA[Maksym Grebenets]]></dc:creator>
            <pubDate>Tue, 05 Feb 2019 00:00:00 GMT</pubDate>
            <atom:updated>2019-03-04T10:21:55.246Z</atom:updated>
            <content:encoded><![CDATA[<h3>Swift Refactoring — Debugging</h3><p>Debugging implementation for <a href="https://swift.org/blog/swift-local-refactoring/">Swift Local Refactoring</a> action.</p><p><a href="https://medium.com/@maksymgrebenets/swift-refactoring-implementation-part-2-d32a1f60278b">Previous — Implementation Part 2</a></p><h3>Ninja</h3><p>If you have built swift-refactor tool using ninja, then you’d have to use command line lldb debugger to debug the code. So after building swift-refactor, just run lldb in the terminal:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/401bcd7369c414de6c3205c22e035edb/href">https://medium.com/media/401bcd7369c414de6c3205c22e035edb/href</a></iframe><p>Next, tell the debugger which executable to debug (swift-refactor in this case):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9ca5f780eef6bd663a441e76620f2879/href">https://medium.com/media/9ca5f780eef6bd663a441e76620f2879/href</a></iframe><p>Then set the breakpoint, e.g. for isApplicable implementation of RefactoringActionCollapseNestedIfStatement class:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fa4e77144bfb371512f6d4fd83113666/href">https://medium.com/media/fa4e77144bfb371512f6d4fd83113666/href</a></iframe><p>Now tell lldb to run the executable with -source-filename and -pos arguments</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aab4cf675a1736238d68ab8d28e42e57/href">https://medium.com/media/aab4cf675a1736238d68ab8d28e42e57/href</a></iframe><p>The debugger has now stopped on line 1714 and is ready for your further input:</p><blockquote>-&gt; 1714</blockquote><p>Same debug commands you’d use in Xcode UI work in command line:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/278384080bf57e7f4a8dd3656d3ce25a/href">https://medium.com/media/278384080bf57e7f4a8dd3656d3ce25a/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*sEBaD4HpmMlUpLEL.gif" /></figure><h3>Xcode</h3><p>If you choose to develop using Xcode, debugging is even easier.</p><p>Edit the <em>swift-refactor</em> scheme</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/205/0*209PoFTv86xfu3ur.png" /></figure><p>and add launch arguments:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/718/0*6pQ9r5DycYI5eiWz.png" /></figure><p>Set breakpoint and hit “Run”:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/797/0*16kowOO3DP2Ei6nj.png" /></figure><p><a href="https://medium.com/@maksymgrebenets/swift-refactoring-summary-faf9bb2d138">Next — Summary</a></p><p><em>Originally published at </em><a href="http://nsbogan.com/swift/2019/02/05/swift-refactoring-debugging"><em>nsbogan.com</em></a><em> on February 5, 2019.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a682b972899a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Swift Refactoring — Implementation — Part 2]]></title>
            <link>https://medium.com/@maksymgrebenets/swift-refactoring-implementation-part-2-d32a1f60278b?source=rss-241c28b5e61b------2</link>
            <guid isPermaLink="false">https://medium.com/p/d32a1f60278b</guid>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Maksym Grebenets]]></dc:creator>
            <pubDate>Mon, 04 Feb 2019 00:00:00 GMT</pubDate>
            <atom:updated>2019-03-04T10:21:29.686Z</atom:updated>
            <content:encoded><![CDATA[<h3>Swift Refactoring — Implementation — Part 2</h3><p>Implementing <a href="https://swift.org/blog/swift-local-refactoring/">Swift Local Refactoring</a> action.</p><p>Part 2 of 2.</p><p>In the <a href="https://medium.com/@maksymgrebenets/swift-refactoring-implementation-part-1-4b2c9352a2c6">previous part</a> we’ve implemented isApplicable check.</p><p>The very same findNestedIfStatements can be reused to implement refactoring transformation.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/496d29523546c04319210ae3e5823dd1/href">https://medium.com/media/496d29523546c04319210ae3e5823dd1/href</a></iframe><p>// 1 The first thing to do is check if refactoring can be applicable. Note that we return true to abort the refactoring transformation.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5dd2b332d6570f97107f0f4fcd53a34d/href">https://medium.com/media/5dd2b332d6570f97107f0f4fcd53a34d/href</a></iframe><p>// 2 Next we create a buffer to write transformed code into and then create an output stream for writing to the buffer.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f62da3fc6f7bb7e9df07f53adef5567a/href">https://medium.com/media/f62da3fc6f7bb7e9df07f53adef5567a/href</a></iframe><p>// 3 A collapsed <em>if</em> statement will still be an <em>if</em> statement, so we write the if keyword to output stream using kw_if keyword token.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/737da4681da8ea6f40ba39da12e68e92/href">https://medium.com/media/737da4681da8ea6f40ba39da12e68e92/href</a></iframe><p>// 4 Now we need to write the combined conditions list to output stream. Conveniently, conditions in the <a href="https://docs.swift.org/swift-book/ReferenceManual/Statements.html#grammar_condition-list">condition-list</a> can be joined together using commas.</p><blockquote><em>Grammar of an if statement</em></blockquote><blockquote><em>if-statement → if condition-list code-block else-clause opt</em></blockquote><blockquote><em>else-clause → else code-block else if-statement condition-list → condition condition , condition-list</em></blockquote><p>Loop through conditions collected while finding nested <em>if</em> statements:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e0e7a9cd18600d033ab78d9d868d7a9d/href">https://medium.com/media/e0e7a9cd18600d033ab78d9d868d7a9d/href</a></iframe><p>Get the range of the condition statement SC in the original source code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fa7cc7aaf27fef7e0ffbd2551d5da168/href">https://medium.com/media/fa7cc7aaf27fef7e0ffbd2551d5da168/href</a></iframe><p>Get the string representation of the condition statement from the original source code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/01ff7650e8373def54ba87f5d809476d/href">https://medium.com/media/01ff7650e8373def54ba87f5d809476d/href</a></iframe><p>SM here is an instance of SourceManager available to each refactoring action implementation.</p><p>Finally write the condition string into output stream joining it with the comma if needed.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/da3c2e976a64060ee97b98b361b6f6f3/href">https://medium.com/media/da3c2e976a64060ee97b98b361b6f6f3/href</a></iframe><p>On first iteration separator will be an empty string while 2nd and consequent statements are joined by &quot;, &quot; string.</p><p>// 5 Now is the time to write the <em>then</em> statement shared by all collapsed <em>if</em> statements. This is the reason we saved LastThenStatement while finding nested if statements. Again, we get the string representation of the <em>then</em> statement and write it to output stream.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d778b6ea9446156b8ac3bdee0b44cf16/href">https://medium.com/media/d778b6ea9446156b8ac3bdee0b44cf16/href</a></iframe><p><em>Then</em> statement already contains opening and closing braces ({ and }), so no need to add those.</p><p>// 6 By this time the new transformed code is saved to output stream OS. Final step is to replace original code with the new code using <em>Source Manager</em> SM.</p><p>First step is to get the range of the untransformed source code.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3a1da469bdc0224e8b1ca47698da9f54/href">https://medium.com/media/3a1da469bdc0224e8b1ca47698da9f54/href</a></iframe><p>Here we take range from the Start of the FirstIfStmt to the End of the <em>then</em> statement.</p><p>That range is then transformed into <em>source</em> range, e.g. range in <em>Source Manager</em> SM’s coordinates:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c2eb44d4c2a6df87963e0c30f5dd5760/href">https://medium.com/media/c2eb44d4c2a6df87963e0c30f5dd5760/href</a></iframe><p>Finally, the code in SourceRange is replaced with contents of Buffer and we return false to indicate successful transformation (🤷‍♂️)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1f4f1306702daf0887709dbdff9a6b3d/href">https://medium.com/media/1f4f1306702daf0887709dbdff9a6b3d/href</a></iframe><p><a href="https://github.com/mgrebenets/swift/blob/feature/collapse-nested-if-statements/lib/IDE/Refactoring.cpp">Full version of Refactoring.cpp</a>.</p><p>The implementation is ready to be tested. If anything goes wrong, then it’s time to debug.</p><p><a href="https://medium.com/@maksymgrebenets/swift-refactoring-debugging-a682b972899a">Next — Debugging</a></p><p><em>Originally published at </em><a href="http://nsbogan.com/swift/2019/02/04/swift-refactoring-implementation-p2"><em>nsbogan.com</em></a><em> on February 4, 2019.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d32a1f60278b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Swift Refactoring — Implementation — Part 1]]></title>
            <link>https://medium.com/@maksymgrebenets/swift-refactoring-implementation-part-1-4b2c9352a2c6?source=rss-241c28b5e61b------2</link>
            <guid isPermaLink="false">https://medium.com/p/4b2c9352a2c6</guid>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Maksym Grebenets]]></dc:creator>
            <pubDate>Mon, 04 Feb 2019 00:00:00 GMT</pubDate>
            <atom:updated>2019-03-04T10:20:53.441Z</atom:updated>
            <content:encoded><![CDATA[<h3>Swift Refactoring — Implementation — Part 1</h3><p>Implementing <a href="https://swift.org/blog/swift-local-refactoring/">Swift Local Refactoring</a> action.</p><p>Part 1 of 2.</p><p><a href="https://medium.com/@maksymgrebenets/swift-refactoring-tests-c30e3694dc49">Previous article — Tests</a></p><h3>Add Refactoring Kind</h3><p>The chosen “Collapse Nested If Statement” refactoring is a cursor-based refactoring, so the first thing to do is to add this code to <a href="https://github.com/apple/swift/blob/master/include/swift/IDE/RefactoringKinds.def">RefactoringKinds.def</a>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/849ec1bc87c984b52efede55c9e2861b/href">https://medium.com/media/849ec1bc87c984b52efede55c9e2861b/href</a></iframe><p>This macro will generate bare bones for new RefactoringActionCollapseNestedIfStatement class.</p><p>A new -collapse-nested-if-statement command line option needs to be added to swift-refactor tool in <a href="https://github.com/apple/swift/blob/master/tools/swift-refactor/swift-refactor.cpp">swift-refactor.cpp</a>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cc37ada3c0dd2fc85164280cd55da499/href">https://medium.com/media/cc37ada3c0dd2fc85164280cd55da499/href</a></iframe><p>Now, to finish off the refactoring action implementation, the following two methods have to be implemented in <a href="https://github.com/apple/swift/blob/master/lib/IDE/Refactoring.cpp">Refactoring.cpp</a>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/12385d6c1b64a28c13d931e3507c6a88/href">https://medium.com/media/12385d6c1b64a28c13d931e3507c6a88/href</a></iframe><p>Both methods require current cursor info for implementation. While isApplication takes it as a CursorInfo input argument, for performChange current cursor info is available as a member of auto-generated RefactoringActionCollapseNestedIfStatement class.</p><h3>Is Applicable</h3><p>The way I approached this implementation task is asking myself a question first.</p><p><em>When is “Collapse Nested If Statements” refactoring action applicable?</em></p><p>The obvious answer is:</p><p><em>When there are more then 1 nested if statements.</em></p><p>So the approach is to <em>find nested if statements</em> at current position and count them.</p><p>Let’s first define a simple structure that will hold information about nested if statements:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fe0c983bd6f677b76c677eafd2bb6067/href">https://medium.com/media/fe0c983bd6f677b76c677eafd2bb6067/href</a></iframe><p>It’s very straightforward, we keep track of the first <em>if</em> statement (FirstIfStmt) and last <strong><em>then</em></strong> statement (LastThenStmt).</p><p>One important thing about collapsible nested <em>if</em> statements is that <strong>they all share a single <em>then</em> statement</strong>, they wouldn’t be collapsible otherwise.</p><p>We also save all the <em>if</em> statement conditions into Conditions list.</p><p>To decide whether a refactoring action is applicable, we need to have non-null first <em>if</em> and last <em>then</em> statements and more than 1 condition in the list:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/25bad23f15152ff364beb0effa5389cb/href">https://medium.com/media/25bad23f15152ff364beb0effa5389cb/href</a></iframe><p>With this new structure in mind, we can define a new helper method that finds all nested <em>if</em> statements under the cursor and then use it to implement isApplicable:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/53efa3bf88d54d480b07593ca7806f02/href">https://medium.com/media/53efa3bf88d54d480b07593ca7806f02/href</a></iframe><h3>Find Nested If Statements</h3><p>This is the part where we finally get down to using cursor info provided to us by refactoring engine.</p><p>It’s easier to reason about the code in terms of <em>declarations</em>, <em>statements</em> and <em>conditions</em>. For example, for a code like this:</p><p>The high level view could be this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*zIDSFQjWM75FzFcr.png" /></figure><p>The (very rough) Abstract Syntax Tree sketch for this code:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*mKYhppMuSdtjrvbi.png" /></figure><p>If the cursor is pointing at the start of the first <em>if</em> statement, then the AST we need to analyze looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*g4ACyT9W8DBEQCbn.png" /></figure><p>So the very first thing to check is to make sure cursor info is pointing at the <em>start of the statement</em> (StmtStart), the refactoring can’t be applied otherwise:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/39db9fead64d7495cd923251a7c89bff/href">https://medium.com/media/39db9fead64d7495cd923251a7c89bff/href</a></iframe><p>The returned empty NestedIfStatementsInfo() indicates that refactoring cannot be applied because NestedIfStatementsInfo().canProceed() is false.</p><h3>Walk the Tree</h3><p>Next thing to do is to start walking Abstract Syntax Tree.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9c3040f26ac9716c954753b490c8215e/href">https://medium.com/media/9c3040f26ac9716c954753b490c8215e/href</a></iframe><p>// 1 For that we create a new type called NestedIfStatementsFinder, which inherits from SourceEntityWalker - the base class for walking the source code tree.</p><p>// 2 We also initialize an instance of NestedIfStatementsFinder called Walker.</p><p>// 3 Additionally, our walker should collect nested if statements info into the Result property.</p><p>// 4 and // 5 Finally tell walker to walk the AST starting from CursorInfo.TrailingStmt and return the result.</p><p>Next let’s have a look at some of the SourceEntityWalker interface.</p><p>It has a handful of methods to walk the AST, specifically it can walk the <em>statement</em>, <em>declaration</em> or <em>expression</em>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3a8990db6a9c39fffaf67a7b45f53151/href">https://medium.com/media/3a8990db6a9c39fffaf67a7b45f53151/href</a></iframe><p>For our implementation we have used bool walk(Stmt *S).</p><p>The type also offers customization points:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b69427d5b934c668c479c7903a8e0e77/href">https://medium.com/media/b69427d5b934c668c479c7903a8e0e77/href</a></iframe><p>These methods can be overridden in types inheriting from SourceEntityWalker. When true is returned, the walker will keep walking the tree, otherwise it will stop.</p><p>For out implementation, we choose to override walkToStmtPre, which is called when walker is <em>about to walk the statement</em>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f89f951f372e65598c943bf98db9aef1/href">https://medium.com/media/f89f951f372e65598c943bf98db9aef1/href</a></iframe><p>Let’s <em>walk</em> this code step by step:</p><p>// 1 We are only interested in nested <em>if</em> statements, so when statement kind (S-&gt;getKind()) is not StmtKind::If then there’s no point to continue (return false).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7eb251a31612f7fafd997537b40acabb/href">https://medium.com/media/7eb251a31612f7fafd997537b40acabb/href</a></iframe><p>// 2 Now that we checked the statement kind, we can safely typecast it to IfStmt.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/64c6cca8d8b62c150639f864824fcb92/href">https://medium.com/media/64c6cca8d8b62c150639f864824fcb92/href</a></iframe><p>// 3 We want to keep track of the first <em>if</em> statement in the Result variable, so save it if this is the first <em>if</em> statement we encounter.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0d59d88b6c1985f376b304c8287bacbd/href">https://medium.com/media/0d59d88b6c1985f376b304c8287bacbd/href</a></iframe><p>// 4 If current <em>if</em> statement has an <em>else</em> statement, it cannot be collapsed, so there’s no point to keep going.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/01ce42d6a5babec93f68f69be81cf65f/href">https://medium.com/media/01ce42d6a5babec93f68f69be81cf65f/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9937574a8a1eba0338106cbfe49386af/href">https://medium.com/media/9937574a8a1eba0338106cbfe49386af/href</a></iframe><p>// 5 We also need to keep track of last <em>then</em> statement, which will be useful while applying refactoring transformation.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a142563337626f36f6b0de2beb3b0a4e/href">https://medium.com/media/a142563337626f36f6b0de2beb3b0a4e/href</a></iframe><p>// 6 If the <em>then</em> statement of current <em>if</em> statement has more than one elements, then the refactoring cannot be applied from this point on and need to stop.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/02dcc45a18b844b90202f347c431ba30/href">https://medium.com/media/02dcc45a18b844b90202f347c431ba30/href</a></iframe><p>Here’s an example of Swift code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/22cf61850fbb48b8314416cd9f19858b/href">https://medium.com/media/22cf61850fbb48b8314416cd9f19858b/href</a></iframe><p>The if a &lt; 2 <em>if</em> statement has 2 elements in the <em>then</em> statement: print(a) and if b &gt; 1 statements.</p><p>// 7 Keep track of the <em>if</em> statement conditions in Conditions list:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9a806b8f4a61c7a286fb5b2aff970b71/href">https://medium.com/media/9a806b8f4a61c7a286fb5b2aff970b71/href</a></iframe><p>// 8 Finally, recursively walk the first statement inside the <em>then</em>-block.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0b3687cd7e036f171ab07930f4df4e03/href">https://medium.com/media/0b3687cd7e036f171ab07930f4df4e03/href</a></iframe><h3>Test</h3><p>Now it’s a good time to run the tests <a href="https://medium.com/@maksymgrebenets/swift-refactoring-tests-c30e3694dc49">created in previous article</a> to make sure implementation of isApplicable works as expected.</p><p><a href="https://medium.com/@maksymgrebenets/swift-refactoring-implementation-part-2-d32a1f60278b">Next — Implementation — Part 2</a></p><p><em>Originally published at </em><a href="http://nsbogan.com/swift/2019/02/04/swift-refactoring-implementation-p1"><em>nsbogan.com</em></a><em> on February 4, 2019.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4b2c9352a2c6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Swift Refactoring — Tests]]></title>
            <link>https://medium.com/@maksymgrebenets/swift-refactoring-tests-c30e3694dc49?source=rss-241c28b5e61b------2</link>
            <guid isPermaLink="false">https://medium.com/p/c30e3694dc49</guid>
            <category><![CDATA[tdd]]></category>
            <dc:creator><![CDATA[Maksym Grebenets]]></dc:creator>
            <pubDate>Sun, 03 Feb 2019 00:00:00 GMT</pubDate>
            <atom:updated>2019-03-04T10:19:25.420Z</atom:updated>
            <content:encoded><![CDATA[<h3>Swift Refactoring — Tests</h3><p>Using TDD approach to implement <a href="https://swift.org/blog/swift-local-refactoring/">Swift Local Refactoring</a> action.</p><p><a href="https://medium.com/@maksymgrebenets/swift-refactoring-setup-f5c8af8fccff">Previous article — Setup</a></p><p>TDD or <a href="https://en.wikipedia.org/wiki/Test-driven_development">Test-driven development</a> is a perfect approach when you face a new and unfamiliar code base.</p><p>It surely helped me, I was able to wind up a large number of tests and then iterate on actual implementation to make those tests pass.</p><p>However, learning how to write tests for Swift source code is a challenge by itself. The project is using lit or <a href="https://llvm.org/docs/CommandGuide/lit.html">LLVM Integrated Tester</a> and you can find more details in Apple’s own <a href="https://github.com/apple/swift/blob/80ce2c3f25b47afb22119ba4510421d46b45dec3/docs/Testing.md#using-litpy">Testing Swift</a> documentation.</p><h3>Test Refactoring Kind</h3><p>I’ll be making changes to the swift-refactor tool in Swift toolchain. For now I will treat that tool as a black box, i.e. I have no idea how the refactoring action is implemented.</p><p>What I do know though, is that if I give swift-refactor tool a file and specify a cursor position in that file, then it will print out a list of applicable refactoring actions at that cursor position.</p><p>So I’ll start by creating a new test file at swift/test/refactoring/RefactoringKind/collapse_nested_if_statement.swift with the following content:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fa8c546b6a72949f94b8e71ba1daa74d/href">https://medium.com/media/fa8c546b6a72949f94b8e71ba1daa74d/href</a></iframe><p>Now, if I point a cursor at row 4 and column 4 in this file, I expect my new refactoring action to be applicable. If I name the refactoring action “Collapse Nested If Statement”, then this text should be the output of the following command:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bd594597bc00f599b37c4123b01003d0/href">https://medium.com/media/bd594597bc00f599b37c4123b01003d0/href</a></iframe><p>So my test case is this:</p><p><em>Given that I run </em><em>swift-refactor for </em><em>collapse_nested_if_statement.swift file, row </em><em>4 and column </em><em>4, I expect the output to contain </em><em>&quot;Collapse Nested If Statement&quot; string.</em></p><p>All I have to do now is write this test case using lit command. This is how it looks:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d102f1d090c2396f822070b78417be8f/href">https://medium.com/media/d102f1d090c2396f822070b78417be8f/href</a></iframe><p>This code is put directly into collapse_nested_if_statement.swift file and lit will pick it up from there.</p><p>Here’s the detailed breakdown of the test spec:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/12d46997f304eb41783f1165943915bb/href">https://medium.com/media/12d46997f304eb41783f1165943915bb/href</a></iframe><p>The CHECK-COLLAPSE-NESTED-IF-STATEMENT is also defined in collapse_nested_if_statement.swift like so:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cb85eea2259ca6cebfe166bf275dcd50/href">https://medium.com/media/cb85eea2259ca6cebfe166bf275dcd50/href</a></iframe><p>So the refactoring action first begins (Action begins), then the Collapse Nested If Statement output is written to standard output and then the action ends (Action ends).</p><p>The special -NEXT syntax is how you can define multi-line strings.</p><p><a href="https://github.com/mgrebenets/swift/blob/feature/collapse-nested-if-statements/test/refactoring/RefactoringKind/collapse_nested_if_statement.swift">More examples of refactoring kind tests</a>.</p><h3>Cursor Position Reference</h3><p>You have probably spotted a small problem with -pos=4:4 argument of the test command.</p><p>If the tests are modified, refactored or moved around, then the actual cursor position of the start of test code can change. When there’s dozens of tests in the single file, fixing up all the impacted -pos= arguments can be quite a challenge.</p><p>That’s where using cursor position <em>reference</em> is really helpful.</p><p>All you have to do is add a block comment like so</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0bad69d0e511c4e75757ff0dc23d1be7/href">https://medium.com/media/0bad69d0e511c4e75757ff0dc23d1be7/href</a></iframe><p>Now you can use &quot;2-statements&quot; string as a reference to the cursor position:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f9543fcaa8292c75c1173ebfaf2806cc/href">https://medium.com/media/f9543fcaa8292c75c1173ebfaf2806cc/href</a></iframe><p>If the test2Statements is relocated in file or some changes happen to formatting or indentation, the tests will still point to the correct cursor position and will require no changes.</p><h3>Test Refactoring Transformation</h3><p>Next thing to test is the refactoring transformation itself.</p><p>The structure of these tests is like this:</p><ul><li>Take the test fixture Swift file that contains code to refactor.</li><li>Pass it to swift-refactor and capture the output.</li><li>Compare output to expected code using diff utility.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*lWkpUDc5_7rsywcD.png" /></figure><p>I have created a new CollapseNestedIfStatement folder under swift/test/refactoring, then created basic.swift file and Outputs/basic directory.</p><p>basic.swift is where the lit tests will live and Outputs/basic is where expected outputs will be created.</p><p>Similar to refactoring kind test, I write the following Swift code in basic.swift:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0bad69d0e511c4e75757ff0dc23d1be7/href">https://medium.com/media/0bad69d0e511c4e75757ff0dc23d1be7/href</a></iframe><p>After the refactoring action has been applied, I expect the code to look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/10685ebd8da129b5bd055d8fbb024f61/href">https://medium.com/media/10685ebd8da129b5bd055d8fbb024f61/href</a></iframe><p>so this is what I put into Outputs/basic/2-statements.swift.expected.</p><p>Now I have to write lit test spec in basic.swift:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/41f193ef30ade80bed08945ff98a3911/href">https://medium.com/media/41f193ef30ade80bed08945ff98a3911/href</a></iframe><p>The test begins by cleaning up output directory with rm -rf %t.result &amp;&amp; mkdir -p %t.result command. %t in this case is expanded to the test name.</p><p>Next the test runs %refactor command which expands to swift-refactor. The command is almost identical to the one in refactoring kind tests, except that this time I pass -collapse-nested-if-statement to tell swift-refactor to apply this particular refactoring action.</p><blockquote><em>The </em><em>-collapse-nested-if-statement command line option hasn’t been implemented yet, but remember that this is TDD.</em></blockquote><p>The output of swift-refactor is saved to %t.result/2-statements.swift.</p><p>Finally, the output is compared to expected Swift code using diff utility.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/378b4c68bb35a6e329bd7b6d9cb19631/href">https://medium.com/media/378b4c68bb35a6e329bd7b6d9cb19631/href</a></iframe><p>-u is used to compare 3 lines of unified context, while -B is used to ignore blank lines.</p><p>%S is expanded to the parent directory of the current (basic.swift) file.</p><p>That’s the basic approach, next step is to add more test code and expected outputs. <a href="https://github.com/mgrebenets/swift/tree/feature/collapse-nested-if-statements/test/refactoring/CollapseNestedIfStatement">Here’s an example</a>.</p><h3>Running Tests</h3><p>Once you have enough tests and made your first changes to implement the new refactoring action, you can rebuild only the swift-refactor tool:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4c5c11e32b2bd12158d81fe148c54421/href">https://medium.com/media/4c5c11e32b2bd12158d81fe148c54421/href</a></iframe><p>where SWIFT_BUILD_DIR is set to location of Swift build folder, for example build/Ninja-RelWithDebInfoAssert+swift-DebugAssert/swift-macosx-x86_64/.</p><p>To speed up the development process even further, you can use --filter option of lit.py to run selected tests only. For example, to run only refactoring kind tests from collapse_nested_if_statement.swift:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7196eb1f3e793cbb4d3b1b321d3706f3/href">https://medium.com/media/7196eb1f3e793cbb4d3b1b321d3706f3/href</a></iframe><p>Or to run refactoring transformation tests from CollapseNestedIfStatement/basic.swift:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ef992ba470906ab782dca0cfaa6c7210/href">https://medium.com/media/ef992ba470906ab782dca0cfaa6c7210/href</a></iframe><p>Now you are all set for the usual “Build, Test, Repeat” development cycle.</p><p><a href="https://medium.com/@maksymgrebenets/swift-refactoring-implementation-part-1-4b2c9352a2c6">Next — Implementation — Part 1</a></p><p><em>Originally published at </em><a href="http://nsbogan.com/swift/2019/02/03/swift-refactoring-tests"><em>nsbogan.com</em></a><em> on February 3, 2019.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c30e3694dc49" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>