private static void mxmlc()

in flex/tools/idea-fcsh-fix/flex3/src/com/intellij/flex/SimpleShellWithFix.java [792:1022]


	private static void mxmlc(String[] args, int id)
	{
		LocalizationManager l10n = ThreadLocalToolkit.getLocalizationManager();

        OutputStream swfOut = null;
		Target s = new Target();
		s.id = id;

		try
		{
            // setup the path resolver
			flex2.compiler.API.usePathResolver();

			// process configuration
			ConfigurationBuffer cfgbuf = new ConfigurationBuffer(CommandLineConfiguration.class, Configuration.getAliases());
			cfgbuf.setDefaultVar(Compiler.FILE_SPECS);
			DefaultsConfigurator.loadDefaults( cfgbuf );
			CommandLineConfiguration configuration = (CommandLineConfiguration) Compiler.processConfiguration(
				ThreadLocalToolkit.getLocalizationManager(), "mxmlc", args, cfgbuf, CommandLineConfiguration.class, Compiler.FILE_SPECS);
			s.configuration = configuration;

			flex2.compiler.API.setupHeadless(configuration);

			if (configuration.benchmark())
			{
				flex2.compiler.API.runBenchmark();
			}
			else
			{
				flex2.compiler.API.disableBenchmark();
			}

			String target = configuration.getTargetFile();
			targets.put("" + id, s);
			s.args = args;

			// make sure targetFile abstract pathname is an absolute path...
			VirtualFile targetFile = flex2.compiler.API.getVirtualFile(target);
			flex2.tools.API.checkSupportedTargetMimeType(targetFile);
			List virtualFileList = flex2.compiler.API.getVirtualFileList(configuration.getFileList()); // List<VirtualFile>

			CompilerConfiguration compilerConfig = configuration.getCompilerConfiguration();
			NameMappings mappings = flex2.compiler.API.getNameMappings(configuration);

            flex2.compiler.Transcoder[] transcoders = flex2.tools.API.getTranscoders( configuration );
            flex2.compiler.Compiler[] compilers = flex2.tools.API.getCompilers(compilerConfig, mappings, transcoders);

			// construct the SWF file name...
            VirtualFile projector = configuration.getProjector();

            if (projector != null && projector.getName().endsWith("avmplus.exe"))
            {
                // output .exe
                s.outputName = configuration.getOutput();
    	        if (s.outputName == null)
    	        {
    		        s.outputName = targetFile.getName();
    		        s.outputName = s.outputName.substring(0, s.outputName.lastIndexOf('.')) + ".exe";
    	        }
            }
            else
            {
                // output SWF
                s.outputName = configuration.getOutput();
    	        if (s.outputName == null)
    	        {
    		        s.outputName = targetFile.getName();
    		        if (projector != null)
    		        {
    		        	s.outputName = s.outputName.substring(0, s.outputName.lastIndexOf('.')) + ".exe";
    		        }
    		        else
    		        {
    		        	s.outputName = s.outputName.substring(0, s.outputName.lastIndexOf('.')) + ".swf";
    		        }
    	        }
            }

			VirtualFile[] asClasspath = compilerConfig.getSourcePath();

			// create a FileSpec...
			s.fileSpec = new FileSpec(Collections.EMPTY_LIST, flex2.tools.API.getFileSpecMimeTypes());

			// create a SourceList
			s.sourceList = new SourceList(virtualFileList,
			                              asClasspath, targetFile, flex2.tools.API.getSourcePathMimeTypes());

			// create a SourcePath...
			s.sourcePath = new SourcePath(asClasspath, targetFile, flex2.tools.API.getSourcePathMimeTypes(),
			                              compilerConfig.allowSourcePathOverlap());

			// create a ResourceContainer...
			s.resources = new ResourceContainer();

			// create a ResourceBundlePath...
			s.bundlePath = new ResourceBundlePath(configuration.getCompilerConfiguration(), targetFile);

			if (ThreadLocalToolkit.getBenchmark() != null)
			{
				ThreadLocalToolkit.getBenchmark().benchmark(l10n.getLocalizedTextString(new InitialSetup()));
			}

			// load SWCs
			s.swcCache = new SwcCache();
			CompilerSwcContext swcContext = new CompilerSwcContext(true, true,
																   configuration.getCompatibilityVersionString());
			swcContext.load( compilerConfig.getLibraryPath(),
							 Configuration.getAllExcludedLibraries(compilerConfig, configuration),
                             compilerConfig.getThemeFiles(),
                             compilerConfig.getIncludeLibraries(),
							 mappings,
							 I18nUtils.getTranslationFormat(compilerConfig),
							 s.swcCache );
			configuration.addExterns( swcContext.getExterns() );
			configuration.addIncludes( swcContext.getIncludes() );
			configuration.getCompilerConfiguration().addDefaultsCssFiles( swcContext.getDefaultsStyleSheets() );
            configuration.getCompilerConfiguration().addThemeCssFiles( swcContext.getThemeStyleSheets() );

			if (ThreadLocalToolkit.getBenchmark() != null)
			{
				ThreadLocalToolkit.getBenchmark().benchmark(l10n.getLocalizedTextString(new LoadedSWCs(swcContext.getNumberLoaded())));
			}

           	s.checksum = cfgbuf.checksum_ts() + swcContext.checksum();

			final SymbolTable symbolTable = SymbolTable.newSymbolTable(configuration);
			s.perCompileData = symbolTable.perCompileData;

			Map licenseMap = configuration.getLicensesConfiguration().getLicenseMap();

            if (projector != null && projector.getName().endsWith("avmplus.exe"))
            {
                // compile
                ThreadLocalToolkit.getPathResolver().addSinglePathResolver(new SourcePathResolver(s.sourcePath));
                List units = flex2.compiler.API.compile(s.fileSpec, s.sourceList, null, s.sourcePath, s.resources, s.bundlePath,
                										swcContext, symbolTable, configuration, compilers,
                										null, licenseMap, new ArrayList());

    			s.units = units;
    			s.sourcePath.clearCache();
    			s.bundlePath.clearCache();
    			s.resources.refresh();

                // link
                s.app = flex2.linker.API.linkConsole(units, new PostLink(configuration), configuration);

                // output .exe
                File file = FileUtil.openFile(s.outputName, true);
                swfOut = new BufferedOutputStream(new FileOutputStream(file));

                flex2.tools.Compiler.createProjector(projector, s.app, swfOut);

                swfOut.flush();
                swfOut.close();

                ThreadLocalToolkit.log(new OutputMessage(s.outputName, Long.toString(file.length())));
            }
            else
            {
                // compile
                ThreadLocalToolkit.getPathResolver().addSinglePathResolver(new SourcePathResolver(s.sourcePath));
    			List units = flex2.compiler.API.compile(s.fileSpec, s.sourceList, null, s.sourcePath, s.resources, s.bundlePath,
    			                                        swcContext, symbolTable, configuration, compilers,
    			                                        new PreLink(), licenseMap, new ArrayList());

    			s.units = units;
    			s.sourcePath.clearCache();
    			s.bundlePath.clearCache();
    			s.resources.refresh();

    			// link
    			s.movie = flex2.linker.API.link(units, new PostLink(configuration), configuration);

                // output SWF
    			File file = FileUtil.openFile(s.outputName, true);
    			swfOut = new BufferedOutputStream(new FileOutputStream(file));

                if (projector != null)
                {
                	flex2.tools.Compiler.createProjector(projector, s.movie, swfOut);
                }
                else
                {
                    flex2.compiler.API.encode(s.movie, swfOut);
                }

    			swfOut.flush();
    			swfOut.close();

    			ThreadLocalToolkit.log(new OutputMessage(s.outputName, Long.toString(file.length())));
            }
		}
		catch (ConfigurationException ex)
		{
			Compiler.processConfigurationException(ex, "mxmlc");
		}
		catch (LicenseException ex)
		{
            ThreadLocalToolkit.logError(ex.getMessage());
		}
		catch (CompilerException ex)
		{
			assert ThreadLocalToolkit.errorCount() > 0;
		}
		catch (LinkerException ex)
		{
			assert ThreadLocalToolkit.errorCount() > 0;
		}
		catch (SwcException ex)
		{
		    assert ThreadLocalToolkit.errorCount() > 0;
		}
		catch (IOException thr) // IOException, Throwable
		{
			ThreadLocalToolkit.logError(thr.getMessage());
			thr.printStackTrace();
		}
		finally
		{
			Benchmark benchmark = ThreadLocalToolkit.getBenchmark();
			if (benchmark != null)
			{
			    benchmark.totalTime();
			    benchmark.peakMemoryUsage(true);
			}

			flex2.compiler.API.removePathResolver();

			if (swfOut != null) try { swfOut.close(); } catch (IOException ioe) {}
		}
	}