Beispiel #1
0
 public void Dispose()
 {
     _ilMerger?.Dispose();
     // Remove temporary exclude file
     if (File.Exists(_excludeFileTmpPath))
     {
         File.Delete(_excludeFileTmpPath);
     }
 }
Beispiel #2
0
        static int Main(string[] args)
        {
            RepackOptions options    = new RepackOptions(args);
            var           logger     = new RepackLogger(options);
            int           returnCode = -1;

            try
            {
                if (options.ShouldShowUsage)
                {
                    Usage();
                    Exit(2);
                }

                ILRepack repack = new ILRepack(options, logger);
                repack.Repack();
                repack.Dispose();
                returnCode = 0;
            }
            catch (RepackOptions.InvalidTargetKindException e)
            {
                Console.WriteLine(e.Message);
                Usage();
                Exit(2);
            }
            catch (Exception e)
            {
                logger.Log(e);
                returnCode = 1;
            }
            finally
            {
                logger.Dispose();
                if (options.PauseBeforeExit)
                {
                    Console.WriteLine("Press Any Key To Continue");
                    Console.ReadKey(true);
                }
            }
            return(returnCode);
        }
Beispiel #3
0
        /// <summary>
        ///     Executes ILRepack with specified options.
        /// </summary>
        /// <returns>Returns true if its successful.</returns>
        public override bool Execute()
        {
            _repackOptions = new RepackOptions
            {
                KeyFile        = _keyFile,
                KeyContainer   = _keyContainer,
                LogFile        = _logFile,
                Log            = !string.IsNullOrEmpty(_logFile),
                LogVerbose     = Verbose,
                UnionMerge     = Union,
                DebugInfo      = DebugInfo,
                CopyAttributes = CopyAttributes,
                AttributeFile  = AttributeFile,
                AllowMultipleAssemblyLevelAttributes = AllowMultiple,
                TargetKind              = _targetKind,
                TargetPlatformVersion   = TargetPlatformVersion,
                TargetPlatformDirectory = TargetPlatformDirectory,
                XmlDocumentation        = XmlDocumentation,
                Internalize             = Internalize,
                RenameInternalized      = RenameInternalized,
                DelaySign = DelaySign,
                AllowDuplicateResources = AllowDuplicateResources,
                AllowZeroPeKind         = ZeroPeKind,
                Parallel        = Parallel,
                PauseBeforeExit = PauseBeforeExit,
                OutputFile      = _outputFile,
                AllowWildCards  = Wildcards
            };

            _ilMerger = new ILRepacking.ILRepack(_repackOptions);

            // Attempt to create output directory if it does not exist.
            var outputPath = Path.GetDirectoryName(OutputFile);

            if (outputPath != null && !Directory.Exists(outputPath))
            {
                try
                {
                    Directory.CreateDirectory(outputPath);
                }
                catch (Exception ex)
                {
                    Log.LogErrorFromException(ex);
                    return(false);
                }
            }

            // Assemblies to be merged.
            var assemblies = new string[_assemblies.Length];

            for (int i = 0; i < _assemblies.Length; i++)
            {
                assemblies[i] = _assemblies[i].ItemSpec;
                if (string.IsNullOrEmpty(assemblies[i]))
                {
                    throw new Exception($"Invalid assembly path on item index {i}");
                }
                if (!File.Exists(assemblies[i]) && !File.Exists(BuildPath(assemblies[i])))
                {
                    throw new Exception($"Unable to resolve assembly '{assemblies[i]}'");
                }
                Log.LogMessage(MessageImportance.High, "Added assembly '{0}'", assemblies[i]);
            }

            // List of assemblies that should not be internalized.
            if (Internalize && InternalizeExclude != null)
            {
                for (int i = 0; i < InternalizeExclude.Length; i++)
                {
                    var exclude = InternalizeExclude[i].ItemSpec;
                    if (string.IsNullOrEmpty(exclude))
                    {
                        continue;
                    }
                    Log.LogMessage(MessageImportance.High,
                                   "Excluding pattern '{0}' from being internalized", exclude);

                    _repackOptions.ExcludeInternalizeMatches.Add(new System.Text.RegularExpressions.Regex(exclude));
                }
            }

            // List of assemblies that should not be internalized.
            if (Internalize && InternalizeExcludeAssemblies != null)
            {
                for (int i = 0; i < InternalizeExcludeAssemblies.Length; i++)
                {
                    var exclude = InternalizeExcludeAssemblies[i].ItemSpec;
                    if (string.IsNullOrEmpty(exclude))
                    {
                        continue;
                    }
                    Log.LogMessage(MessageImportance.High,
                                   "Excluding assembly '{0}' from being internalized", exclude);

                    _repackOptions.ExcludeInternalizeAssemblies.Add(exclude);
                }
            }

            _repackOptions.InputAssemblies = assemblies;

            // Path that will be used when searching for assemblies to merge.
            var searchPath = new List <string> {
                "."
            };

            searchPath.AddRange(LibraryPath.Select(iti => BuildPath(iti.ItemSpec)));
            _repackOptions.SearchDirectories = searchPath.ToArray();

            // Attempt to merge assemblies.
            try
            {
                Log.LogMessage(MessageImportance.High, "Merging {0} assemb{1} to '{2}'",
                               _assemblies.Length, _assemblies.Length != 1 ? "ies" : "y", _outputFile);

                // Measure performance
                Stopwatch stopWatch = new Stopwatch();
                stopWatch.Start();

                _ilMerger.Repack();
                stopWatch.Stop();

                Log.LogMessage(MessageImportance.High, "Merge succeeded in {0} s", stopWatch.Elapsed.TotalSeconds);
            }
            catch (Exception e)
            {
                _ilMerger?.Dispose();
                _ilMerger = null;
                Log.LogErrorFromException(e, true);
                return(false);
            }

            return(true);
        }