1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.IO;
using System.Collections.Specialized;

//Author: Erik Bergstedt ( http://blackandodd.blogspot.se/ )
//License: Use as you wish, but keep it sane else Half-Life 3 will never be released
//see https://developers.google.com/closure/compiler/docs/api-ref for more help
//for browser input see http://closure-compiler.appspot.com/home
namespace GoogleClosureHelper
{
    public static class ClosureHelper
    {
        /// <summary>
        /// Put all your .js files in this folder.
        /// </summary>
        public static string InputFolder { get; set; }
        private static string _outputFile = @"CompiledFromGoogleClosureCompiler.js";
        /// <summary>
        /// The compiled file. Defaults to "CompiledFromGoogleClosureCompiler.js" in the root of your project.
        /// </summary>
        public static string OutputFile
        {
            get
            {
                return _outputFile;
            }
            set
            {
                if (!value.EndsWith(".js"))
                    value += ".js";
                _outputFile = value;
            }
        }

        public static bool CompileToOutputPath(string compilation_level, string output_format, string output_info)
        {            
            var request = WebRequest.Create("http://closure-compiler.appspot.com/compile");
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";            

            String[] files;
            try 
          {
                files = Directory.GetFiles(InputFolder, @"*.js", SearchOption.TopDirectoryOnly);
          }
          catch (Exception)
          {    
            throw;
          }

            string JS = "";
            foreach (var item in files)
            {
                try
                {                    
                    JS += System.IO.File.ReadAllText(item);
                }
                catch (Exception)
                {
                    Console.WriteLine("Could not read " + item);
                }
            }

            NameValueCollection userdata = new NameValueCollection();
            userdata.Add("js_code", JS);
            userdata.Add("compilation_level", compilation_level);
            userdata.Add("output_format", output_format);
            userdata.Add("output_info", output_info);

            using (var wc = new WebClient())
            {
                try
                {
                    byte[] response = wc.UploadValues("http://closure-compiler.appspot.com/compile",
                        "POST",
                        userdata);

                    File.WriteAllText(_outputFile, System.Text.Encoding.UTF8.GetString(response));
                }                
                catch (Exception)
                {
                    Console.WriteLine("Failed to get response, http://closure-compiler.appspot.com/compile is probably down.");
                }
            }
            return true;
        }
    }
}