Example #1
0
        /// <summary>
        /// Entry point
        /// </summary>
        internal static int Main(string[] args)
        {
            try
            {
#if DEBUG
                if (args.Length == 0)
                {
                    var assemblyFolder          = Path.GetDirectoryName(typeof(Program).Assembly.Location);
                    var folder                  = Path.GetFullPath(Path.Combine(assemblyFolder, @"..\..\..\..\Sources\Framework\Generated"));
                    var sdkRoot                 = Path.GetFullPath(Path.Combine(assemblyFolder, @"..\..\..\..\Binaries"));
                    var xmlFolder               = Path.GetFullPath(Path.Combine(assemblyFolder, @"..\..\..\Android\Docs\xml"));
                    var forwardAssembliesFolder = Path.GetFullPath(Path.Combine(assemblyFolder, @"..\..\..\..\Sources\Framework\ForwardAssemblies"));
                    //var sdkRoot = Environment.GetEnvironmentVariable("ANDROID_SDK_ROOT");
                    if (string.IsNullOrEmpty(sdkRoot))
                    {
                        throw new ArgumentException("Set ANDROID_SDK_ROOT environment variable");
                    }
                    var platformFolder = Path.Combine(sdkRoot, @"platforms\android-15");
                    args = new[] {
                        ToolOptions.InputJar.CreateArg(Path.Combine(platformFolder, "Android.jar")),
                        ToolOptions.InputAttrsXml.CreateArg(Path.Combine(platformFolder, @"data\res\values\attrs.xml")),
                        ToolOptions.InputSourceProperties.CreateArg(Path.Combine(platformFolder, "source.properties")),
                        ToolOptions.OutputFolder.CreateArg(folder),
                        //ToolOptions.DoxygenXmlFolder.CreateArg(xmlFolder),
                        //ToolOptions.PublicKeyToken.CreateArg("0a72796057571e65"),
                        ToolOptions.ForwardAssembliesFolder.CreateArg(forwardAssembliesFolder)
                    };
                }
#endif

                var options = new CommandLineOptions(args);
                if (options.ShowHelp)
                {
                    options.Usage();
                    return(2);
                }

                if (!File.Exists(options.FrameworkJar))
                {
                    throw new ArgumentException(string.Format("Framework jar ({0}) not found.", options.FrameworkJar));
                }

                if (!File.Exists(options.SourceProperties))
                {
                    throw new ArgumentException(string.Format("Source.properties ({0}) not found.", options.SourceProperties));
                }
                var sdkPropertiesPath = Path.Combine(Path.GetDirectoryName(options.SourceProperties), "sdk.properties");
                if (!File.Exists(sdkPropertiesPath))
                {
                    throw new ArgumentException(string.Format("sdk.properties ({0}) not found.", sdkPropertiesPath));
                }

                // Load source.properties
                var sourceProperties = new SourceProperties(options.SourceProperties);

                using (var jf = new JarFile(File.Open(options.FrameworkJar, FileMode.Open, FileAccess.Read), AttributeConstants.Dot42Scope, null))
                {
                    // Create output folder
                    var folder = Path.Combine(options.OutputFolder, sourceProperties.PlatformVersion);
                    Directory.CreateDirectory(folder);

                    if (!options.VersionOnly)
                    {
                        // Load Doxygen model
                        var xmlModel = new DocModel();
                        using (Profiler.Profile(x => Console.WriteLine("Load XML took {0}ms", x.TotalMilliseconds)))
                        {
                            xmlModel.Load(options.DoxygenXmlFolder);
                        }

                        // Create mscorlib
                        CreateFrameworkAssembly(jf, xmlModel, sourceProperties, folder);

                        // Copy AndroidManifest.xml into the assembly.
                        var manifestStream = jf.GetResource("AndroidManifest.xml");

                        // Copy resources.arsc into the assembly.
                        var resourcesStream = jf.GetResource("resources.arsc");

                        // Load attrs.xml into memory
                        var attrsXml = File.ReadAllBytes(options.AttrsXml);

                        // Load layout.xml into memory
                        var layoutXml = File.ReadAllBytes(Path.Combine(folder, "layout.xml"));

                        // Create base package
                        var basePackagePath = Path.Combine(folder, "base.apk");
                        using (var fileStream = new FileStream(basePackagePath, FileMode.Create, FileAccess.Write))
                        {
                            using (var zipStream = new ZipOutputStream(fileStream)
                            {
                                UseZip64 = UseZip64.Off
                            })
                            {
                                zipStream.SetLevel(9);

                                zipStream.PutNextEntry(new ZipEntry("AndroidManifest.xml")
                                {
                                    CompressionMethod = CompressionMethod.Deflated
                                });
                                zipStream.Write(manifestStream, 0, manifestStream.Length);
                                zipStream.CloseEntry();

                                zipStream.PutNextEntry(new ZipEntry("resources.arsc")
                                {
                                    CompressionMethod = CompressionMethod.Deflated
                                });
                                zipStream.Write(resourcesStream, 0, resourcesStream.Length);
                                zipStream.CloseEntry();

                                zipStream.PutNextEntry(new ZipEntry(@"attrs.xml")
                                {
                                    CompressionMethod = CompressionMethod.Deflated
                                });
                                zipStream.Write(attrsXml, 0, attrsXml.Length);
                                zipStream.CloseEntry();

                                zipStream.PutNextEntry(new ZipEntry(@"layout.xml")
                                {
                                    CompressionMethod = CompressionMethod.Deflated
                                });
                                zipStream.Write(layoutXml, 0, layoutXml.Length);
                                zipStream.CloseEntry();
                            }
                        }

                        // Create output folder file
                        if (!string.IsNullOrEmpty(options.OutputFolderFile))
                        {
                            File.WriteAllText(options.OutputFolderFile, folder);
                        }
                    }

                    // Create output version file
                    var version =
                        string.Format(File.ReadAllText(Path.Combine(folder, "..", "..", "Header.txt")), "Version.cs") + Environment.NewLine +
                        string.Format("[assembly: System.Reflection.AssemblyVersion(\"{0}\")]", sourceProperties.AssemblyVersion) + Environment.NewLine +
                        string.Format("[assembly: System.Reflection.AssemblyFileVersion(\"{0}\")]", sourceProperties.AssemblyFileVersion) + Environment.NewLine +
                        string.Format("[assembly: System.Reflection.AssemblyInformationalVersion(\"{0}\")]", sourceProperties.AssemblyInformationalVersion) + Environment.NewLine +
                        "#if !BASELIB" + Environment.NewLine +
                        string.Format("[assembly: System.Runtime.Versioning.TargetFramework(\"Dot42,Version={0}\", FrameworkDisplayName = \"Dot42\")]", sourceProperties.PlatformVersion) + Environment.NewLine +
                        "#endif" + Environment.NewLine;
                    File.WriteAllText(Path.Combine(folder, "Version.cs"), version);

                    if (!options.VersionOnly)
                    {
                        // Load sdk.properties
                        var sdkProperties = new SdkProperties(sdkPropertiesPath);

                        // Create framework ini
                        var frameworkIni = new FrameworkIni(folder);
                        Initialize(frameworkIni, sourceProperties, sdkProperties);
                        frameworkIni.Save();

                        // Create FrameworkList.xml
                        FrameworkListBuilder.Build(folder, options.ForwardAssembliesFolder, null,
                                                   sourceProperties.AssemblyVersion, sourceProperties.PlatformVersion);
                    }
                }

                return(0);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
#if DEBUG
                Console.WriteLine(ex.StackTrace);
#endif
                return(1);
            }
        }