Example #1
0
    /// <summary>
    /// Builds a stub EXE that executes the "0install run" command at a specific path.
    /// </summary>
    /// <param name="path">The path to store the generated EXE file.</param>
    /// <param name="target">The application to be launched.</param>
    /// <param name="command">The command argument to be passed to the the "0install run" command; can be <c>null</c>.</param>
    /// <param name="gui"><c>true</c> to build a GUI stub, <c>false</c> to build a CLI stub.</param>
    /// <exception cref="OperationCanceledException">The user canceled the task.</exception>
    /// <exception cref="InvalidOperationException">There was a compilation error while generating the stub EXE.</exception>
    /// <exception cref="IOException">A problem occurred while writing to the filesystem.</exception>
    /// <exception cref="WebException">A problem occurred while downloading additional data (such as icons).</exception>
    /// <exception cref="UnauthorizedAccessException">Write access to the filesystem is not permitted.</exception>
    public void BuildRunStub(string path, FeedTarget target, string?command = null, bool gui = false)
    {
        #region Sanity checks
        if (string.IsNullOrEmpty(path))
        {
            throw new ArgumentNullException(nameof(path));
        }
        #endregion

        var compilerParameters = new CompilerParameters
        {
            OutputAssembly        = path,
            GenerateExecutable    = true,
            TreatWarningsAsErrors = true,
            ReferencedAssemblies  = { "System.dll" },
            CompilerOptions       = gui ? "/target:winexe" : "/target:exe"
        };

        string?iconPath = GetIconPath(target, command);
        if (iconPath != null)
        {
            compilerParameters.CompilerOptions += " /win32icon:" + iconPath.EscapeArgument();
        }

        compilerParameters.CompileCSharp(
            GetCode(
                exe: GetBinary(gui),
                arguments: GetArguments(target.Uri, command, gui),
                title: target.Feed.GetBestName(CultureInfo.CurrentUICulture, command)),
            Manifest);
    }
Example #2
0
        /// <summary>
        /// Builds a stub EXE that executes the "0install run" command.
        /// </summary>
        /// <param name="target">The application to be launched via the stub.</param>
        /// <param name="path">The target path to store the generated EXE file.</param>
        /// <param name="handler">A callback object used when the the user is to be informed about the progress of long-running operations such as downloads.</param>
        /// <param name="needsTerminal"><c>true</c> to build a CLI stub, <c>false</c> to build a GUI stub.</param>
        /// <param name="command">The command argument to be passed to the the "0install run" command; can be <c>null</c>.</param>
        /// <exception cref="OperationCanceledException">The user canceled the task.</exception>
        /// <exception cref="InvalidOperationException">There was a compilation error while generating the stub EXE.</exception>
        /// <exception cref="IOException">A problem occurs while writing to the filesystem.</exception>
        /// <exception cref="WebException">A problem occured while downloading additional data (such as icons).</exception>
        /// <exception cref="UnauthorizedAccessException">Write access to the filesystem is not permitted.</exception>
        internal static void BuildRunStub(FeedTarget target, [NotNull] string path, [NotNull] ITaskHandler handler, bool needsTerminal, [CanBeNull] string command = null)
        {
            #region Sanity checks
            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentNullException(nameof(path));
            }
            if (handler == null)
            {
                throw new ArgumentNullException(nameof(handler));
            }
            #endregion

            var compilerParameters = new CompilerParameters
            {
                GenerateExecutable      = true,
                OutputAssembly          = path,
                IncludeDebugInformation = false,
                GenerateInMemory        = false,
                TreatWarningsAsErrors   = true,
                ReferencedAssemblies    = { "System.dll" }
            };

            if (!needsTerminal)
            {
                compilerParameters.CompilerOptions += " /target:winexe";
            }

            var icon = target.Feed.GetIcon(Icon.MimeTypeIco, command);
            if (icon != null)
            {
                string iconPath = IconCacheProvider.GetInstance().GetIcon(icon.Href, handler);
                compilerParameters.CompilerOptions += " /win32icon:" + iconPath.EscapeArgument();
            }

            compilerParameters.CompileCSharp(
                GetRunStubCode(target, needsTerminal, command),
                typeof(StubBuilder).GetEmbeddedString("Stub.manifest"));
        }
        /// <summary>
        /// Builds a stub EXE that executes the "0install run" command.
        /// </summary>
        /// <param name="target">The application to be launched via the stub.</param>
        /// <param name="path">The target path to store the generated EXE file.</param>
        /// <param name="iconStore">Stores icon files downloaded from the web as local files.</param>
        /// <param name="needsTerminal"><c>true</c> to build a CLI stub, <c>false</c> to build a GUI stub.</param>
        /// <param name="command">The command argument to be passed to the the "0install run" command; can be <c>null</c>.</param>
        /// <exception cref="OperationCanceledException">The user canceled the task.</exception>
        /// <exception cref="InvalidOperationException">There was a compilation error while generating the stub EXE.</exception>
        /// <exception cref="IOException">A problem occurred while writing to the filesystem.</exception>
        /// <exception cref="WebException">A problem occurred while downloading additional data (such as icons).</exception>
        /// <exception cref="UnauthorizedAccessException">Write access to the filesystem is not permitted.</exception>
        internal static void BuildRunStub(FeedTarget target, string path, IIconStore iconStore, bool needsTerminal, string?command = null)
        {
            #region Sanity checks
            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentNullException(nameof(path));
            }
            if (iconStore == null)
            {
                throw new ArgumentNullException(nameof(iconStore));
            }
            #endregion

            var compilerParameters = new CompilerParameters
            {
                OutputAssembly        = path,
                GenerateExecutable    = true,
                TreatWarningsAsErrors = true,
                ReferencedAssemblies  = { "System.dll" },
                CompilerOptions       = needsTerminal ? "/target:exe" : "/target:winexe"
            };

            var icon = target.Feed.GetIcon(Icon.MimeTypeIco, command);
            if (icon != null)
            {
                try
                {
                    string iconPath = iconStore.GetPath(icon);
                    new System.Drawing.Icon(iconPath).Dispose(); // Try to parse icon to ensure it is valid
                    compilerParameters.CompilerOptions += " /win32icon:" + iconPath.EscapeArgument();
                }
                #region Error handling
                catch (UriFormatException ex)
                {
                    Log.Warn(ex);
                }
                catch (WebException ex)
                {
                    Log.Warn(ex);
                }
                catch (IOException ex)
                {
                    Log.Warn($"Failed to store {icon}");
                    Log.Warn(ex);
                }
                catch (UnauthorizedAccessException ex)
                {
                    Log.Warn($"Failed to store {icon}");
                    Log.Warn(ex);
                }
                catch (ArgumentException ex)
                {
                    Log.Warn($"Failed to parse {icon}");
                    Log.Warn(ex);
                }
                #endregion
            }

            compilerParameters.CompileCSharp(
                code: GetRunStubCode(target, needsTerminal, command),
                manifest: typeof(StubBuilder).GetEmbeddedString("Stub.manifest"));
        }