public static string GetShortPath(string longPath)
        {
            // Nmake - 1980s technology...
            StringBuilder shortPath = new StringBuilder(longPath.Length + 1);

            if (0 == ArduinoPathHelper.GetShortPathName(longPath, shortPath, shortPath.Capacity))
            {
                return(longPath);
            }

            return(shortPath.ToString());
        }
 public static string GetArduinoVariantPathShort(string variant)
 {
     return(ArduinoPathHelper.GetShortPath(ArduinoPathHelper.GetArduinoVariantPath(variant)));
 }
        public void regenerateMakefile()
        {
            var names            = typeof(ArduinoProjectHelper).Assembly.GetManifestResourceNames();
            var streamOfTemplate = typeof(ArduinoProjectHelper).Assembly.GetManifestResourceStream(ManifestPrefix + "." + kManifestStringName);

            if (streamOfTemplate == null || streamOfTemplate.Length == 0)
            {
                throw new Exception("Expecting template in resources");
            }

            ArduinoBoardInfo currentVariant = ArduinoVariantInfo;

            using (StreamWriter newMakeFile = new StreamWriter(_pathToMakeFile, false))
            {
                // squirt out the basics
                newMakeFile.WriteLine("ARDUINO_INSTALL=" + ArduinoPathHelper.ArduinoPathShort);
                newMakeFile.WriteLine("ARDUINO_FIRMWARE=" + ArduinoPathHelper.ArduinoFirmwarePathShort);
                newMakeFile.WriteLine("ARDUINO_TOOLS=" + ArduinoPathHelper.ArduinoToolsPathShort);
                newMakeFile.WriteLine("VARIANT_PATH=" + ArduinoPathHelper.GetArduinoVariantPathShort(ArduinoVariant));

                // Write out CPP & c files
                var cppFiles = getArduinoFiles("*.cpp");
                var cFiles   = getArduinoFiles("*.c");

                // Find libraries
                var libraries = extractLibrariesFromIno();

                foreach (var lib in libraries)
                {
                    foreach (var cppFile in lib.CPPSourceFiles)
                    {
                        cppFiles.Add(cppFile);
                    }

                    foreach (var cFile in lib.CSourceFiles)
                    {
                        cFiles.Add(cFile);
                    }

                    newMakeFile.WriteLine("CFLAGS= $(CFLAGS) -I " + ArduinoPathHelper.GetShortPath(lib.headerPath));
                    newMakeFile.WriteLine("CXXFLAGS= $(CXXFLAGS) -I " + ArduinoPathHelper.GetShortPath(lib.headerPath));
                }

                newMakeFile.Write("CXXSRC=");
                foreach (var cppFile in cppFiles)
                {
                    newMakeFile.WriteLine("\t\\");
                    newMakeFile.Write(string.Format("\t{0}", ArduinoPathHelper.GetShortPath(cppFile)));
                }
                newMakeFile.WriteLine("");

                newMakeFile.Write("CSRC=");

                foreach (var cFile in cFiles)
                {
                    newMakeFile.WriteLine("\t\\");
                    newMakeFile.Write(string.Format("\t{0}", ArduinoPathHelper.GetShortPath(cFile)));
                }
                newMakeFile.WriteLine("");

                if (_pathToIno != null)
                {
                    newMakeFile.Write("INOSRC=\\");
                    newMakeFile.Write(string.Format("\t{0}", ArduinoPathHelper.GetShortPath(_pathToIno)));

                    newMakeFile.WriteLine("");
                }

                // Write out OBJs

                newMakeFile.Write("OBJ=");
                foreach (var cppFile in cppFiles)
                {
                    var objName = Path.GetFileNameWithoutExtension(cppFile) + ".obj";
                    newMakeFile.WriteLine("\t\\");
                    newMakeFile.Write(string.Format("\t$(O)\\{0}", objName));
                }
                foreach (var cFile in cFiles)
                {
                    var objName = Path.GetFileNameWithoutExtension(cFile) + ".obj";

                    newMakeFile.WriteLine("\t\\");
                    newMakeFile.Write(string.Format("\t$(O)\\{0}", objName));
                }

                if (_pathToIno != null)
                {
                    var objName = Path.GetFileNameWithoutExtension(_pathToIno) + ".obj";

                    newMakeFile.WriteLine("\t\\");
                    newMakeFile.Write(string.Format("\t$(O)\\{0}", objName));
                }

                newMakeFile.WriteLine("");
                newMakeFile.WriteLine("");

                newMakeFile.WriteLine(string.Format("TARGET={0}", Target));
                newMakeFile.WriteLine(string.Format("O={0}", Intermediate));
                newMakeFile.WriteLine("");
                newMakeFile.WriteLine(string.Format("MCU={0}", currentVariant.MCU));
                newMakeFile.WriteLine(string.Format("F_CPU={0}", currentVariant.F_CPU));
                newMakeFile.WriteLine(string.Format("VARIANT={0}", currentVariant.VARIANT));
                newMakeFile.WriteLine(string.Format("DEF_CPU={0}", currentVariant.DEF_CPU));
                newMakeFile.WriteLine(string.Format("D_USB_VID=USB_VID={0}", currentVariant.USB_VID));
                newMakeFile.WriteLine(string.Format("D_USB_PID=USB_PID={0}", currentVariant.USB_PID));
                newMakeFile.WriteLine("");


                using (StreamReader templateReader = new StreamReader(streamOfTemplate))
                {
                    while (!templateReader.EndOfStream)
                    {
                        string readLine = templateReader.ReadLine();
                        newMakeFile.WriteLine(readLine);
                    }
                }
            }
        }