nitc iOS: quiet output of `xcodebuild`
[nit.git] / src / platform / ios.nit
index 94d0c5f..0cb1ce0 100644 (file)
@@ -18,7 +18,7 @@ module ios
 import platform
 import compiler::abstract_compiler
 import xcode_templates
-private import annotation
+import app_annotations
 
 redef class ToolContext
        redef fun platform_from_name(name)
@@ -36,30 +36,28 @@ private class IOSPlatform
        redef fun toolchain(toolcontext, compiler) do return new IOSToolchain(toolcontext, compiler)
 end
 
+private class IosProject
+       super AppProject
+
+       redef fun namespace do return super.to_camel_case
+end
+
 private class IOSToolchain
        super MakefileToolchain
 
-       # Root of the iOS project, usually `.nit_compile/ios/`
+       # Root of the iOS project, usually `nit_compile/ios/`
        var ios_project_root: String is noinit
 
-       redef fun default_outname do return "{super}.app"
+       # `app.nit` project for the current compilation target
+       var app_project = new IosProject(compiler.modelbuilder, compiler.mainmodule) is lazy
 
-       # Name of the current project of `compiler`
-       fun project_name: String
-       do
-               var project_name = null
-               # TODO unite the app_name annotation from Android with iOS
-               var annot = compiler.modelbuilder.lookup_annotation_on_modules("app_name", compiler.mainmodule)
-               if annot != null then project_name = annot.arg_as_string(compiler.modelbuilder)
-               if project_name == null then project_name = compiler.mainmodule.name
-               return project_name
-       end
+       redef fun default_outname do return "{super}.app"
 
-       # Compile C files in `ios_project_root/project_name`
+       # Compile C files in `ios_project_root/app_project.name`
        redef fun compile_dir
        do
-               ios_project_root = super/"ios"
-               return ios_project_root/project_name
+               ios_project_root = root_compile_dir/"ios"
+               return ios_project_root/app_project.short_name
        end
 
        redef fun write_files(compile_dir, cfiles)
@@ -73,7 +71,63 @@ private class IOSToolchain
 
        redef fun write_makefile(compile_dir, cfiles)
        do
-               var project_name = project_name
+               var project_name = app_project.short_name
+
+               # ---
+               # project_folder (source code)
+
+               # Create the plist in the same directory as the generated C code
+               if not compile_dir.file_exists then compile_dir.mkdir
+               var plist = new PlistTemplate(app_project.name, app_project.namespace,
+                       app_project.version, app_project.version_code.to_s)
+               plist.write_to_file compile_dir/"Info.plist"
+
+               # Copy the folder `ios/AppIcon.appiconset` from the root of the project
+               var project_root = "."
+               var mpackage = compiler.mainmodule.first_real_mmodule.mpackage
+               if mpackage != null then
+                       var root = mpackage.root
+                       if root != null then
+                               var filepath = root.filepath
+                               if filepath != null then
+                                       project_root = filepath
+                               end
+                       end
+               end
+
+               # Copy all resources
+               var app_files = [project_root]
+               app_files.add_all app_project.files
+
+               var icons_found = false
+
+               for path in app_files do
+                       var icon_dir = path / "ios" / "AppIcon.appiconset"
+                       if icon_dir.file_exists then
+                               icons_found = true
+
+                               # Prepare the `Assets.xcassets` folder
+                               var target_assets_dir = compile_dir / "Assets.xcassets"
+                               if not target_assets_dir.file_exists then target_assets_dir.mkdir
+
+                               """
+{
+  "info" : {
+       "version" : 1,
+       "author" : "nitc"
+  }
+}""".write_to_file target_assets_dir / "Contents.json"
+
+                               # copy the res folder to the compile dir
+                               icon_dir = icon_dir.realpath
+                               toolcontext.exec_and_check(["cp", "-R", icon_dir, target_assets_dir], "iOS project error")
+                       end
+               end
+
+               # TODO Register asset files
+
+               # ---
+               # project_folder.xcodeproj (projet meta data)
 
                # Create an XCode project directory
                var dir = ios_project_root/project_name+".xcodeproj"
@@ -85,38 +139,52 @@ private class IOSToolchain
                ## Register all source files
                for file in cfiles do pbx.add_file new PbxFile(file)
                for file in compiler.extern_bodies do
-                       pbx.add_file new PbxFile(file.filename.basename(""))
+                       pbx.add_file new PbxFile(file.filename.basename)
                end
 
-               ## TODO Register asset files
+               # Basic storyboard, mainly to have the right screen size
+               var launch_screen_storyboard = new LaunchScreenStoryboardTemplate
+               launch_screen_storyboard.title = app_project.name
+               launch_screen_storyboard.subtitle = "app.nit"
+               launch_screen_storyboard.write_to_file ios_project_root / "LaunchScreen.storyboard"
 
-               pbx.write_to_file dir/"project.pbxproj"
+               # Register the Assets.xcassets folder in the project description
+               if icons_found then
+                       var xcassets = new PbxFile("Assets.xcassets")
+                       pbx.add_file xcassets
+               end
 
-               # Create the plist in the same directory as the generated C code
-               if not compile_dir.file_exists then compile_dir.mkdir
-               var plist = new PlistTemplate("org.nitlanguage") # TODO customize using an annotation
-               plist.write_to_file compile_dir/"Info.plist"
+               pbx.write_to_file dir / "project.pbxproj"
        end
 
        redef fun compile_c_code(compile_dir)
        do
-               var project_name = project_name
+               var project_name = app_project.short_name
                var release = toolcontext.opt_release.value
                var outfile = outfile(compiler.mainmodule)
 
                # Compile with `xcodebuild`
                #
                # TODO support more than the iPhone and the simulator.
+               var compile_mode = if release then "Release" else "Debug"
                var args = ["sh", "-c", "cd {ios_project_root}; " +
-                       "xcodebuild -target '{project_name}' " +
+                       "xcodebuild -quiet -target '{project_name}' " +
                        "-destination 'platform=iOS Simulator,name=iPhone' " +
-                       "-configuration {if release then "Release" else "Debug"} " +
+                       "-configuration {compile_mode} " +
+                        "ONLY_ACTIVE_ARCH=NO "+
                        "-sdk iphonesimulator build"]
                toolcontext.exec_and_check(args, "iOS project error")
 
                # Move compiled app to destination
-               if outfile.file_exists then outfile.rmdir
-               args = ["mv", "{ios_project_root}/build/Debug-iphonesimulator/{project_name}.app", outfile]
+               if outfile.file_exists then
+                       var error = outfile.rmdir
+                       if error != null then
+                               print_error error
+                               exit 1
+                       end
+               end
+
+               args = ["mv", "{ios_project_root}/build/{compile_mode}-iphonesimulator/{project_name}.app", outfile]
                toolcontext.exec_and_check(args, "iOS project error")
        end
 end