Skip to content

Commit

Permalink
Checked in basic project
Browse files Browse the repository at this point in the history
  • Loading branch information
Xpitfire committed May 29, 2017
1 parent d9b6811 commit 6b3b04c
Show file tree
Hide file tree
Showing 11 changed files with 1,166 additions and 1 deletion.
11 changes: 11 additions & 0 deletions .idea/artifacts/migrator.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions .idea/dictionaries/mcd.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

902 changes: 902 additions & 0 deletions .idea/workspace.xml

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions .idea/xtextAutoBuilderState.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: Program

26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,25 @@
# TargetFrameworkMigrator
# TargetFrameworkMigrator
Simple tool to migrate the target framework for Visual Studio Projects Solution wide.

## Requirements
[Java 8 or higher](https://java.com/en/download/)

## Usage
[Download](https://github.com/Xpitfire/TargetFrameworkMigrator/releases) release java file.

Open command prompt or PowerShell and execute:
```
java -jar migrator.jar -path=<path-to-root-dir> -recursive=<true/false> -version=<version-string>
```

Parameter | Description
--- | ---
-path | root path of the solution
-version | parameter for defining the destination path
-recursive | optional: by default recursive is set to false, to prevent unwanted recursive descent

## Example

```
java -jar MonitoringRenamer.jar -path=C:/Worspace/Demo -recursive=true -version=v4.6.2
```
11 changes: 11 additions & 0 deletions TargetFrameworkMigrator.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
164 changes: 164 additions & 0 deletions src/Program.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.*;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

/**
* Created by xpitfire on 28/05/2017.
*/
public class Program {

private static final String LOGGER_FILE_NAME = "TargetFrameworkMigrator.log";

private static Logger logger;

private static String path = null;
private static boolean recursive = false;
private static String version = null;

public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException {

initLogger();

try {
if (args == null
|| args.length < 1) {
System.err.println("Invalid parameter list!");
System.err.println("Missing path and text prefix.");
printErrorHelp();
return;
}

Map<String, String> paramMap = new HashMap<>();

int idx = 0;

while (idx < args.length) {
String[] keyValue = args[idx].split("=");
if (keyValue.length < 2) continue;
paramMap.put(keyValue[0], keyValue[1]);
idx++;
}

// mandatory settings

if (paramMap.containsKey("-path")) {
path = paramMap.get("-path");
} else {
System.err.println("Invalid version parameter!");
printErrorHelp();
return;
}

if (paramMap.containsKey("-version")) {
version = paramMap.get("-version");
} else {
System.err.println("Invalid path parameter!");
printErrorHelp();
return;
}

// optional settings

if (paramMap.containsKey("-recursive")) {
recursive = Boolean.parseBoolean(paramMap.get("-recursive"));
}

System.out.printf("Initialized path=%s, version=%s recursive=%b\n", path, version, recursive);
final File folder = new File(path);

System.out.println("Scanning directories...");
scanAndUpdateFiles(folder);

} catch (Exception ex) {

System.err.println("Application crashed: " + ex.getMessage());
logger.log(Level.SEVERE, "Application crashed", ex);

}
}

private static void printErrorHelp() {
System.err.println("usage> java TargetFrameworkMigrator.jar -path=<path-to-root-dir> -version=<version-string> -recursive=<true/false>");
System.err.println("example> java TargetFrameworkMigrator.jar -path=C:\\test -version=v4.6.2 -recursive=true");
logger.severe("Invalid application parameter> " +
String.format("-path=%s, -version=%s, -recursive=%b -\n",
path, version, recursive));
}

private static void scanAndUpdateFiles(final File folder)
throws IOException, SAXException, ParserConfigurationException, XPathExpressionException, TransformerException {

File[] files = folder.listFiles();
if (files == null) return;

System.out.println(folder + ":");
for (final File fileEntry : files) {
if (fileEntry.isDirectory() && recursive) {
scanAndUpdateFiles(fileEntry);
} else if (fileEntry.isFile()) {
String file = fileEntry.getName();
if (getFileExtension(file).equals(".csproj")) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(fileEntry);
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression expr = xpath.compile("/Project/PropertyGroup/TargetFrameworkVersion/text()");
NodeList nodeList = (NodeList)expr.evaluate(doc, XPathConstants.NODESET);
if (nodeList.getLength() < 1) continue;
Node node = nodeList.item(0);
logger.info("Found: " + fileEntry.getAbsolutePath() + " version:" + node.getTextContent());
node.setTextContent(version);
Transformer xformer = TransformerFactory.newInstance().newTransformer();
xformer.transform(new DOMSource(doc), new StreamResult(new File(fileEntry.getAbsolutePath())));
logger.info("Changed: " + fileEntry.getAbsolutePath() + " version:" + node.getTextContent());
}
}
}
System.out.println();
}

private static String getFileExtension(String fileName) {
String extension = "";
int i = fileName.lastIndexOf('.');
if (i >= 0) {
extension = "." + fileName.substring(i + 1);
}
return extension;
}

private static void initLogger() {
logger = Logger.getLogger("TargetFrameworkMigrator");
FileHandler fh;
try {

// This block configure the logger with handler and formatter
fh = new FileHandler(LOGGER_FILE_NAME);
logger.addHandler(fh);
SimpleFormatter formatter = new SimpleFormatter();
fh.setFormatter(formatter);

} catch (SecurityException | IOException e) {
e.printStackTrace();
}
}

}

0 comments on commit 6b3b04c

Please sign in to comment.