Exemplo n.º 1
0
    bool Compile(CompilerOptions options)
    {
        if (this.fPrintTargets)
        {
            Console.WriteLine(Localize("Compiling", options.strOutputFileName));
        }

        VsaEngine engine = new Microsoft.JScript.Vsa.VsaEngine();

        if (null == engine)
        {
            throw new CmdLineException(CmdLineError.CannotCreateEngine, JScriptCompiler.GetCultureInfo());
        }
        engine.InitVsaEngine("JSC://Microsoft.JScript.Vsa.VsaEngine", new EngineSite(options));
        engine.LCID = JScriptCompiler.LCID;
        engine.GenerateDebugInfo = options.fDebug;

        engine.SetOption("AutoRef", options.autoRef);
        engine.SetOption("fast", options.fFast);
        engine.SetOption("output", options.strOutputFileName);
        engine.SetOption("PEFileKind", options.PEFileKind);
        engine.SetOption("print", options.fPrint);
        engine.SetOption("libpath", options.libpath);
        if (options.versionInfo != null)
        {
            engine.SetOption("version", options.versionInfo);
        }
        engine.SetOption("VersionSafe", options.fVersionSafe);
        engine.SetOption("defines", options.Defines);
        engine.SetOption("warnaserror", options.fTreatWarningsAsErrors);
        engine.SetOption("WarningLevel", options.nWarningLevel);
        if (options.ManagedResources.Count > 0)
        {
            engine.SetOption("managedResources", options.ManagedResources.Values);
        }

        bool fStdlibAdded   = false;
        bool fWinFormsAdded = false;

        foreach (string assemblyName in options.ImportFileNames)
        {
            this.AddAssemblyReference(engine, assemblyName);
            string filename = Path.GetFileName(assemblyName);
            if (String.Compare(filename, "mscorlib.dll", true, CultureInfo.InvariantCulture) == 0)
            {
                fStdlibAdded = true;
            }
            else if (String.Compare(filename, "System.Windows.Forms.dll", true, CultureInfo.InvariantCulture) == 0)
            {
                fWinFormsAdded = true;
            }
        }

        // Only add mscorlib if it hasn't already been added.
        if (!options.fNoStdlib && !fStdlibAdded)
        {
            AddAssemblyReference(engine, "mscorlib.dll");
        }
        // add System.Windows.Forms if target is winexe and it hasn't already been added
        if ((options.PEFileKind == PEFileKinds.WindowApplication) && !options.fNoStdlib && !fWinFormsAdded)
        {
            AddAssemblyReference(engine, "System.Windows.Forms.dll");
        }

        for (int j = 0; j < options.SourceFileNames.Count; j++)
        {
            AddSourceFile(engine, (string)options.SourceFileNames[j], options.codepage, options.fForceCodepage);
        }

        bool isCompiled = false;

        try{
            isCompiled = engine.Compile();
        }catch (VsaException e) {
            // check for expected errors
            if (e.ErrorCode == VsaError.AssemblyExpected)
            {
                if (e.InnerException != null && e.InnerException is System.BadImageFormatException)
                {
                    // the reference was not for an assembly
                    CmdLineException cmdLineError = new CmdLineException(CmdLineError.InvalidAssembly, e.Message, JScriptCompiler.GetCultureInfo());
                    Console.WriteLine(cmdLineError.Message);
                }
                else if (e.InnerException != null && (e.InnerException is System.IO.FileNotFoundException || e.InnerException is System.IO.FileLoadException))
                {
                    // the referenced file not valid
                    CmdLineException cmdLineError = new CmdLineException(CmdLineError.AssemblyNotFound, e.Message, JScriptCompiler.GetCultureInfo());
                    Console.WriteLine(cmdLineError.Message);
                }
                else
                {
                    CmdLineException cmdLineError = new CmdLineException(CmdLineError.InvalidAssembly, JScriptCompiler.GetCultureInfo());
                    Console.WriteLine(cmdLineError.Message);
                }
            }
            else if (e.ErrorCode == VsaError.SaveCompiledStateFailed)
            {
                CmdLineException cmdLineError = new CmdLineException(CmdLineError.ErrorSavingCompiledState, e.Message, JScriptCompiler.GetCultureInfo());
                Console.WriteLine(cmdLineError.Message);
            }
            else if (e.ErrorCode == VsaError.AssemblyNameInvalid && e.InnerException != null)
            {
                CmdLineException cmdLineError = new CmdLineException(CmdLineError.InvalidCharacters, e.Message, JScriptCompiler.GetCultureInfo());
                Console.WriteLine(cmdLineError.Message);
            }
            else
            {
                Console.WriteLine(Localize("INTERNAL COMPILER ERROR"));
                Console.WriteLine(e);
            }
            return(false);
        }catch (Exception e) {
            Console.WriteLine(Localize("INTERNAL COMPILER ERROR"));
            Console.WriteLine(e);
            return(false);
        }
        return(isCompiled);
    }
Exemplo n.º 2
0
  bool Compile(CompilerOptions options){
    if (this.fPrintTargets)
      Console.WriteLine(Localize("Compiling", options.strOutputFileName));

    VsaEngine engine = new Microsoft.JScript.Vsa.VsaEngine();
    if (null == engine)
      throw new CmdLineException(CmdLineError.CannotCreateEngine, JScriptCompiler.GetCultureInfo());
    engine.InitVsaEngine("JSC://Microsoft.JScript.Vsa.VsaEngine", new EngineSite(options));
    engine.LCID = JScriptCompiler.GetCultureInfo().LCID;
    engine.GenerateDebugInfo = options.fDebug;

    engine.SetOption("ReferenceLoaderAPI", "ReflectionOnlyLoadFrom");
    engine.SetOption("AutoRef", options.autoRef);
    engine.SetOption("fast", options.fFast);
    engine.SetOption("output", options.strOutputFileName);
    engine.SetOption("PEFileKind", options.PEFileKind);
    engine.SetOption("PortableExecutableKind", options.PEKindFlags);
    engine.SetOption("ImageFileMachine", options.PEMachineArchitecture);
    engine.SetOption("print", options.fPrint);
    engine.SetOption("libpath", options.libpath);
    if (options.versionInfo != null)
      engine.SetOption("version", options.versionInfo);
    engine.SetOption("VersionSafe", options.fVersionSafe);
    engine.SetOption("defines", options.Defines);
    engine.SetOption("warnaserror", options.fTreatWarningsAsErrors);
    engine.SetOption("WarningLevel", options.nWarningLevel);
    if (options.ManagedResources.Count > 0)
      engine.SetOption("managedResources", options.ManagedResources.Values);

    bool fStdlibAdded = false;
    bool fWinFormsAdded = false;
    foreach (string assemblyName in options.ImportFileNames){
      AddAssemblyReference(engine, assemblyName);
      string filename = Path.GetFileName(assemblyName);
      if (String.Compare(filename, "mscorlib.dll", StringComparison.OrdinalIgnoreCase) == 0)
        fStdlibAdded = true;
      else if (String.Compare(filename, "System.Windows.Forms.dll", StringComparison.OrdinalIgnoreCase) == 0)
        fWinFormsAdded = true;
    }

    // Only add mscorlib if it hasn't already been added.
    if (!options.fNoStdlib && !fStdlibAdded)
      AddAssemblyReference(engine, "mscorlib.dll");
    // add System.Windows.Forms if target is winexe and it hasn't already been added
    if ((options.PEFileKind == PEFileKinds.WindowApplication) && !options.fNoStdlib && !fWinFormsAdded)
      AddAssemblyReference(engine, "System.Windows.Forms.dll");

    for (int j = 0; j < options.SourceFileNames.Count; j++)
      AddSourceFile(engine, (string)options.SourceFileNames[j], options.codepage);

    bool isCompiled = false;
    try{
      isCompiled = engine.Compile();
    }catch(VsaException e){
      // check for expected errors
      if (e.ErrorCode == VsaError.AssemblyExpected){
        if (e.InnerException != null && e.InnerException is System.BadImageFormatException){
          // the reference was not for an assembly
          CmdLineException cmdLineError = new CmdLineException(CmdLineError.InvalidAssembly, e.Message, JScriptCompiler.GetCultureInfo());
          Console.WriteLine(cmdLineError.Message);
        }else if (e.InnerException != null && (e.InnerException is System.IO.FileNotFoundException || e.InnerException is System.IO.FileLoadException)){
          // the referenced file not valid
          CmdLineException cmdLineError = new CmdLineException(CmdLineError.AssemblyNotFound, e.Message, JScriptCompiler.GetCultureInfo());
          Console.WriteLine(cmdLineError.Message);
        }else{
          CmdLineException cmdLineError = new CmdLineException(CmdLineError.InvalidAssembly, JScriptCompiler.GetCultureInfo());
          Console.WriteLine(cmdLineError.Message);
        }
      }else if (e.ErrorCode == VsaError.SaveCompiledStateFailed){
        CmdLineException cmdLineError = new CmdLineException(CmdLineError.ErrorSavingCompiledState, e.Message, JScriptCompiler.GetCultureInfo());
        Console.WriteLine(cmdLineError.Message);
      }else if (e.ErrorCode == VsaError.AssemblyNameInvalid && e.InnerException != null){
        CmdLineException cmdLineError = new CmdLineException(CmdLineError.InvalidCharacters, e.Message, JScriptCompiler.GetCultureInfo());
        Console.WriteLine(cmdLineError.Message);
      }else{
        Console.WriteLine(Localize("INTERNAL COMPILER ERROR"));
        Console.WriteLine(e);
      }
      return false;
    }catch(Exception e){
      Console.WriteLine(Localize("INTERNAL COMPILER ERROR"));
      Console.WriteLine(e);
      return false;
    }catch{
      Console.WriteLine(Localize("INTERNAL COMPILER ERROR"));
      return false;
    }
    return isCompiled;
  }