public int Generate(string wszInputFilePath, string bstrInputFileContents, string wszDefaultNamespace,
                                IntPtr[] pbstrOutputFileContents, out uint pbstrOutputFileContentSize, IVsGeneratorProgress pGenerateProgress)
            {
                // wait for debugger on var
                DebuggerHelper.WaitForDebuggerIfEnabled("NFRESXCODEGEN_DEBUG");

                if (bstrInputFileContents == null)
                {
                    throw new ArgumentNullException(bstrInputFileContents);
                }
                codeFilePath          = wszInputFilePath;
                codeFileNameSpace     = wszDefaultNamespace;
                codeGeneratorProgress = pGenerateProgress;

                byte[] bytes = GenerateCode(wszInputFilePath, bstrInputFileContents);
                if (bytes == null)
                {
                    pbstrOutputFileContents[0] = IntPtr.Zero;
                    pbstrOutputFileContentSize = 0;
                }
                else
                {
                    pbstrOutputFileContents[0] = Marshal.AllocCoTaskMem(bytes.Length);
                    Marshal.Copy(bytes, 0, pbstrOutputFileContents[0], bytes.Length);
                    pbstrOutputFileContentSize = (uint)bytes.Length;
                }
                return(COM_HResults.S_OK);
            }
예제 #2
0
        public override bool Execute()
        {
            // report to VS output window what step the build is
            Log.LogCommandLine(MessageImportance.Normal, "Starting nanoFramework MetaDataProcessor...");

            // wait for debugger on var
            DebuggerHelper.WaitForDebuggerIfEnabled(TasksConstants.BuildTaskDebugVar);

            try
            {
                // execute the ToolTask base method which is running the command line to call MetaDataProcessor will the appropriate parameters
                base.Execute();

                RecordFilesWritten();
            }
            catch (Exception ex)
            {
                Log.LogError("nanoFramework build error: " + ex.Message);
            }
            finally
            {
                Cleanup();
            }

            // if we've logged any errors that's because there were errors (WOW!)
            return(!Log.HasLoggedErrors);
        }
        public override bool Execute()
        {
            // report to VS output window what step the build is
            Log.LogMessage(MessageImportance.Normal, "Generating binary output file...");

            // wait for debugger on var
            DebuggerHelper.WaitForDebuggerIfEnabled(TasksConstants.BuildTaskDebugVar);

            // default with null, indicating that we've generated nothing
            FileWritten = null;

            // get paths for PE files
            // rename extension .dll with .pe
            List <string> peCollection = new List <string>();

            peCollection = AssemblyReferences?.Select(a => { return(a.GetMetadata("FullPath").Replace(".dll", ".pe").Replace(".exe", ".pe")); }).ToList();

            // add executable PE
            peCollection.Add(AssemblyPE);

            // get executable path and file name
            // rename executable extension .exe with .bin
            var binOutputFile = Assembly.Replace(".exe", ".bin");

            using (FileStream binFile = new FileStream(binOutputFile, FileMode.Create))
            {
                // now we will re-deploy all system assemblies
                foreach (string peItem in peCollection)
                {
                    // append to the deploy blob the assembly
                    using (FileStream fs = File.Open(peItem, FileMode.Open, FileAccess.Read))
                    {
                        long   length = (fs.Length + 3) / 4 * 4;
                        byte[] buffer = new byte[length];

                        fs.Read(buffer, 0, (int)fs.Length);

                        // copy this assembly to the bin file too
                        binFile.Write(buffer, 0, (int)length);
                    }
                }
            }

            // bin file written
            FileWritten = new TaskItem(binOutputFile);

            return(true);
        }
        public override bool Execute()
        {
            // report to VS output window what step the build is
            Log.LogMessage(MessageImportance.Normal, "Generating nanoResources nanoFramework assembly...");

            // wait for debugger on var
            DebuggerHelper.WaitForDebuggerIfEnabled(TasksConstants.BuildTaskDebugVar);

            try
            {
                // If there are no sources to process, just return (with success) and report the condition.
                if ((Sources == null) || (Sources.Length == 0))
                {
                    Log.LogMessage(MessageImportance.Low, "GenerateResource.NoSources");

                    // Indicate we generated nothing
                    OutputResources = null;

                    return(true);
                }

                if (!ValidateParameters())
                {
                    // Indicate we generated nothing
                    OutputResources = null;
                    return(false);
                }

                // In the case that OutputResources wasn't set, build up the outputs by transforming the Sources
                if (!CreateOutputResourcesNames())
                {
                    // Indicate we generated nothing
                    OutputResources = null;
                    return(false);
                }

                // First we look to see if we have a resgen linked files cache.  If so, then we can use that
                // cache to speed up processing.
                ReadStateFile();

                bool nothingOutOfDate = true;

                List <ITaskItem> inputsToProcess  = new List <ITaskItem>();
                List <ITaskItem> outputsToProcess = new List <ITaskItem>();

                // decide what sources we need to build
                for (int i = 0; i < Sources.Length; ++i)
                {
                    // Attributes from input items are forwarded to output items.
                    //Sources[i].CopyMetadataTo(OutputResources[i]);

                    if (!File.Exists(Sources[i].ItemSpec))
                    {
                        // Error but continue with the files that do exist
                        Log.LogError("GenerateResource.ResourceNotFound", Sources[i].ItemSpec);
                        UnsuccessfullyCreatedOutFiles.Add(OutputResources[i].ItemSpec);
                    }
                    else
                    {
                        // check to see if the output resources file (and, if it is a .resx, any linked files)
                        // is up to date compared to the input file
                        if (ShouldRebuildResgenOutputFile(Sources[i].ItemSpec, OutputResources[i].ItemSpec))
                        {
                            nothingOutOfDate = false;

                            inputsToProcess.Add(Sources[i]);
                            outputsToProcess.Add(OutputResources[i]);
                        }
                    }
                }

                if (nothingOutOfDate)
                {
                    Log.LogMessage("GenerateResource.NothingOutOfDate");
                }
                else
                {
                    // Prepare list of referenced assemblies
                    AssemblyName[] assemblyList;
                    try
                    { //only load system.drawing, mscorlib.  no parameters needed here?!!
                        assemblyList = LoadReferences();
                    }
                    catch (ArgumentException e)
                    {
                        Log.LogError("GenerateResource.ReferencedAssemblyNotFound - {0}: {1}", e.ParamName, e.Message);
                        OutputResources = null;
                        return(false);
                    }

                    // always create a separate AppDomain because an assembly would be locked.
                    AppDomain appDomain = null;

                    ProcessResourceFiles process = null;

                    try
                    {
                        appDomain = AppDomain.CreateDomain
                                    (
                            "generateResourceAppDomain",
                            null,
                            AppDomain.CurrentDomain.SetupInformation
                                    );

                        object obj = appDomain.CreateInstanceFromAndUnwrap
                                     (
                            typeof(ProcessResourceFiles).Module.FullyQualifiedName,
                            typeof(ProcessResourceFiles).FullName
                                     );

                        Type processType = obj.GetType();

                        process = (ProcessResourceFiles)obj;


                        //setup strongly typed class name??

                        process.Run(Log, assemblyList, (ITaskItem[])inputsToProcess.ToArray(), (ITaskItem[])outputsToProcess.ToArray(),
                                    UseSourcePath);

                        if (null != process.UnsuccessfullyCreatedOutFiles)
                        {
                            foreach (string item in process.UnsuccessfullyCreatedOutFiles)
                            {
                                UnsuccessfullyCreatedOutFiles.Add(item);
                            }
                        }
                        process = null;
                    }
                    finally
                    {
                        if (appDomain != null)
                        {
                            AppDomain.Unload(appDomain);
                            process   = null;
                            appDomain = null;
                        }
                    }
                }

                // And now we serialize the cache to save our resgen linked file resolution for later use.
                WriteStateFile();

                RemoveUnsuccessfullyCreatedResourcesFromOutputResources();

                RecordFilesWritten();
            }
            catch (Exception ex)
            {
                Log.LogError("nanoFramework GenerateNanoResourceTask error: " + ex.Message);
            }

            // if we've logged any errors that's because there were errors (WOW!)
            return(!Log.HasLoggedErrors);
        }