[Java] automatically generates Visual Studio project files

Source: Internet
Author: User

Visual Studio can only add files one by one when adding source code, which is sometimes troublesome. The following tool is used to automatically generate Visual Studio project files.

What is the use of this tool? You will know when you want to use Visual Studio to read the Linux kernel code.

 

Projectcreator. Java

package wsq;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowEvent;import java.awt.event.WindowListener;import java.io.File;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.util.Date;import java.util.Properties;import javax.swing.JButton;import javax.swing.JFileChooser;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JTextField;import javax.swing.filechooser.FileFilter;public class ProjectCreator {public static void main(String[] args) throws IOException {final JFrame frame = new JFrame("ProjectCreator");frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setSize(600, 400);frame.setLocation(200, 100);frame.getContentPane().setLayout(new java.awt.GridBagLayout());JLabel lb = new JLabel();lb.setText("Select project path:");final JTextField path = new JTextField(20);JButton btnBrows = new JButton("...");btnBrows.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {JFileChooser dlg = new JFileChooser(path.getText());FileFilter filter = new FileFilter() {@Overridepublic String getDescription() {return null;}@Overridepublic boolean accept(File f) {return f.isDirectory();}};dlg.setFileFilter(filter);dlg.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);if (dlg.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION) {try {path.setText(dlg.getSelectedFile().getCanonicalPath());} catch (IOException e1) {e1.printStackTrace();}}}});JButton btnRun = new JButton();btnRun.setLocation(60, 60);btnRun.setText("Run");btnRun.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent arg0) {try {// key functionnew ProjectProc().process(path.getText());JOptionPane.showMessageDialog(frame, "finished");} catch (Exception e) {JOptionPane.showMessageDialog(frame, e.toString());}}});frame.getContentPane().add(lb);frame.getContentPane().add(path);frame.getContentPane().add(btnBrows);frame.getContentPane().add(btnRun);frame.setVisible(true);final Properties prop = new Properties();try {prop.load(new FileReader(ProjectProc.getConfigFileName()));} catch (IOException e) {e.printStackTrace();}path.setText(prop.getProperty("path"));frame.addWindowListener(new WindowListener() {@Overridepublic void windowClosing(WindowEvent arg0) {try {prop.setProperty("path", path.getText());String file = ProjectProc.getConfigFileName();prop.store(new FileWriter(file), (new Date()).toString());} catch (IOException e) {e.printStackTrace();}}@Overridepublic void windowActivated(WindowEvent e) {}@Overridepublic void windowClosed(WindowEvent e) {}@Overridepublic void windowDeactivated(WindowEvent e) {}@Overridepublic void windowDeiconified(WindowEvent e) {}@Overridepublic void windowIconified(WindowEvent e) {}@Overridepublic void windowOpened(WindowEvent e) {}});}}

 

Projectproc. Java

Package WSQ; import Java. io. bufferedreader; import Java. io. file; import Java. io. filewriter; import Java. io. inputstream; import Java. io. inputstreamreader; import Java. util. collections; import Java. util. comparator; import Java. util. using list; import Java. util. UUID; public class projectproc {class filecomp implements comparator <file >{@ overridepublic int compare (File O1, file O2) {return o1.getpath (). compareto (O2.getpath () ;}} static string [] g_ayext = {"CPP", "H", "cxx", "C", "Java", "HPP ", "hxx", "CS", "php"}; static rule list <string> g_llext = new rule list <string> (); public static string getconfigfilename () {file F = new file ("myconfig. conf "); string Path = f. getabsolutepath (); return path;} private Boolean issourcefile (string path) {If (g_llext.size () = 0) {for (string E: g_ayext) {g_llext.add (E );}} Int idx = path. lastindexof ('. '); If (idx <= 0) {return false;} string ext = path. substring (idx + 1); If (g_llext.contains (EXT) {return true;} else {return false ;}} public void process (string path) throws exception {bufferedreader BR = NULL; filewriter fw = NULL; try {string full = ""; inputstream Templ = getclass (). getresourceasstream ("template.vcproj.txt"); BR = new bufferedreader (New inputstreamreader (Templ); While (BR. ready () {full + = BR. readline () + "\ r \ n";} file F = new file (PATH); Path = f. getabsolutepath (); If (! Path. endswith (file. separator) {Path = path + file. separator;} string projname = path. substring (path. lastindexof (file. separator, path. length ()-2) + 1, path. length ()-1); // key functionstring result = searchdir (PATH); Result = result. replace (path, ""); Full = full. replace ("{name}", projname); Full = full. replace ("{guid}", "{" + UUID. randomuuid (). tostring () + "}"); Full = full. replace ("{files}", re Sult); string resfile = path + projname + ". vcproj "; fw = new filewriter (resfile); FW. write (full. tochararray (); FW. flush ();} catch (exception e) {e. printstacktrace (); throw E;} finally {If (BR! = NULL) {Br. Close () ;}if (FW! = NULL) {FW. Close () ;}}/*** @ Param path * absolute path * @ return string */private string searchdir (string path) {If (! Path. endswith (file. separator) {Path = path + file. separator;} string name = path. substring (path. lastindexof (file. separator, path. length ()-2) + 1, path. length ()-1); string result = string. format ("<Filter Name = \" % s \ "filter = \" \ ">", name); file dir = new file (PATH ); file [] files = dir. listfiles (); If (Files = NULL) {result + = "</filter>"; return result;} your list <File> List = new external list <File> (); for (File file: Files) {list. add (File);} filecomp c = new filecomp (); collections. sort (list, c); For (File file: List) {If (file. ishidden () {// do nothing, ignoresystem. out. println ("file is hidden:" + file. getpath (); continue;} If (file. getname (). startswith (". ") {// do nothing, ignoresystem. out. println ("file is start '. ': "+ file. getpath (); continue;} If (file. isdirectory () {string sub = searchdir (file. getpath (); Result + = sub;} else {If (issourcefile (file. getpath () {string sub = string. format ("<file relativepath = \" % s \ "> </File> \ r \ n", file. getpath (); Result + = sub ;}} result + = "</filter>"; return result ;}}

 

Template File: template.vcproj.txt

<?xml version="1.0" encoding="gb2312"?><VisualStudioProjectProjectType="Visual C++"Version="7.10"Name="{{NAME}}"ProjectGUID="{{GUID}}"Keyword="Win32Proj"><Platforms><PlatformName="Win32"/></Platforms><Configurations><ConfigurationName="Debug|Win32"OutputDirectory="Debug"IntermediateDirectory="Debug"ConfigurationType="1"CharacterSet="2"><ToolName="VCCLCompilerTool"Optimization="0"PreprocessorDefinitions="WIN32;_DEBUG;_WINDOWS"MinimalRebuild="TRUE"BasicRuntimeChecks="3"RuntimeLibrary="5"UsePrecompiledHeader="0"WarningLevel="3"Detect64BitPortabilityProblems="TRUE"DebugInformationFormat="4"/><ToolName="VCCustomBuildTool"/><ToolName="VCLinkerTool"OutputFile="$(OutDir)/test.exe"LinkIncremental="2"GenerateDebugInformation="TRUE"ProgramDatabaseFile="$(OutDir)/test.pdb"SubSystem="2"TargetMachine="1"/><ToolName="VCMIDLTool"/><ToolName="VCPostBuildEventTool"/><ToolName="VCPreBuildEventTool"/><ToolName="VCPreLinkEventTool"/><ToolName="VCResourceCompilerTool"/><ToolName="VCWebServiceProxyGeneratorTool"/><ToolName="VCXMLDataGeneratorTool"/><ToolName="VCWebDeploymentTool"/><ToolName="VCManagedWrapperGeneratorTool"/><ToolName="VCAuxiliaryManagedWrapperGeneratorTool"/></Configuration><ConfigurationName="Release|Win32"OutputDirectory="Release"IntermediateDirectory="Release"ConfigurationType="1"CharacterSet="2"><ToolName="VCCLCompilerTool"PreprocessorDefinitions="WIN32;NDEBUG;_WINDOWS"RuntimeLibrary="4"UsePrecompiledHeader="0"WarningLevel="3"Detect64BitPortabilityProblems="TRUE"DebugInformationFormat="3"/><ToolName="VCCustomBuildTool"/><ToolName="VCLinkerTool"OutputFile="$(OutDir)/test.exe"LinkIncremental="1"GenerateDebugInformation="TRUE"SubSystem="2"OptimizeReferences="2"EnableCOMDATFolding="2"TargetMachine="1"/><ToolName="VCMIDLTool"/><ToolName="VCPostBuildEventTool"/><ToolName="VCPreBuildEventTool"/><ToolName="VCPreLinkEventTool"/><ToolName="VCResourceCompilerTool"/><ToolName="VCWebServiceProxyGeneratorTool"/><ToolName="VCXMLDataGeneratorTool"/><ToolName="VCWebDeploymentTool"/><ToolName="VCManagedWrapperGeneratorTool"/><ToolName="VCAuxiliaryManagedWrapperGeneratorTool"/></Configuration></Configurations><References></References><Files><!--<FilterName="Source Files"Filter="cpp;c;cxx;def;odl;idl;hpj;bat;asm;asmx"UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"></Filter><FilterName="Header Files"Filter="h;hpp;hxx;hm;inl;inc;xsd"UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"></Filter><FilterName="Resource Files"Filter="rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx"UniqueIdentifier="{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}"></Filter>--><!--<Filter Name="core" Filter=""><File RelativePath=".\core\TimerHandler.h"></File></Filter>-->{{FILES}}</Files><Globals></Globals></VisualStudioProject>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.