Created
November 11, 2016 08:30
-
-
Save alexnavratil/3bead01caed9ea3152be01dabbde0bbf to your computer and use it in GitHub Desktop.
Extracts Angular 2 inlined templates into seperate html files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedWriter; | |
import java.io.IOException; | |
import java.nio.charset.Charset; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import java.nio.file.Paths; | |
import java.util.ArrayList; | |
import java.util.List; | |
/** | |
* Created by anavratil on 11.11.2016. | |
*/ | |
public class Main { | |
public static void main(String[] args) throws IOException { | |
List<Path> fileList = new ArrayList<>(); | |
recursiveListFiles(Paths.get(".")).stream() | |
.filter(path -> path.getFileName().toString().endsWith(".component.ts")) | |
.forEach(fileList::add); | |
fileList.stream() | |
.map(path -> { | |
try { | |
//path, lines as list, original content, template extraction | |
return new Object[]{path, Files.readAllLines(path), null, null}; | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return null; | |
}) | |
.filter(obj -> obj != null) | |
.map(strings -> new Object[]{strings[0], strings[1], String.join("\n", (List<String>)strings[1]), null}) | |
.forEach(obj -> { | |
System.out.println(((Path)obj[0]).getFileName().toString()); | |
String content = (String)obj[2]; | |
int startIndex = content.indexOf("template: `"); | |
if(startIndex >= 0){ | |
startIndex += "template: `".length(); | |
int endIndex = content.indexOf('`', startIndex); | |
if(endIndex >= 0){ | |
String template = content.substring(startIndex, endIndex); | |
Path originalPath = (Path)obj[0]; | |
Path templatePath = Paths.get(originalPath.getParent().toString(), (originalPath.getFileName().toString().replace(".component.ts", ".component.html"))); | |
String relativeTemplatePath = templatePath.toString().replace(originalPath.getParent().toString(), ".").replace('\\', '/'); | |
content = content.replace("template: `"+template+"`", "templateUrl: '"+relativeTemplatePath+"'"); | |
try (BufferedWriter writer = Files.newBufferedWriter(templatePath, Charset.forName("UTF-8"))) { | |
writer.write(template, 0, template.length()); | |
} catch (IOException x) { | |
x.printStackTrace(); | |
} | |
try { | |
Files.delete(originalPath); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
try (BufferedWriter writer = Files.newBufferedWriter(originalPath, Charset.forName("UTF-8"))) { | |
writer.write(content, 0, content.length()); | |
} catch (IOException x) { | |
x.printStackTrace(); | |
} | |
} | |
} | |
}); | |
} | |
private static List<Path> recursiveListFiles(Path directory) throws IOException { | |
List<Path> pathList = new ArrayList<>(); | |
Files.list(directory) | |
.forEach(path -> { | |
if(Files.isDirectory(path)){ | |
try { | |
pathList.addAll(recursiveListFiles(path)); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} else { | |
pathList.add(path); | |
} | |
}); | |
return pathList; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment