ZeroInstall.Publish.EntryPoints.InterpretedScript.HasShebang C# (CSharp) Method

HasShebang() protected method

Determines whether a file is executable and has a shebang line pointing to a specific interpreter.
protected HasShebang ( [ file, [ interpreter ) : bool
file [ The file to analyze.
interpreter [ The name of the interpreter to search for (e.g. 'python').
return bool
        protected bool HasShebang([NotNull] FileInfo file, [NotNull, Localizable(false)] string interpreter)
        {
            #region Sanity checks
            if (file == null) throw new ArgumentNullException(nameof(file));
            if (string.IsNullOrEmpty(interpreter)) throw new ArgumentNullException(nameof(interpreter));
            #endregion

            if (!IsExecutable(file.FullName)) return false;

            string firstLine = file.ReadFirstLine(Encoding.ASCII);
            if (string.IsNullOrEmpty(firstLine)) return false;
            return
                firstLine.StartsWith(@"#!/usr/bin/" + interpreter) ||
                firstLine.StartsWith(@"#!/usr/bin/env " + interpreter);
        }
        #endregion