1 package org.codehaus.mojo.rspec;
2
3 import java.net.MalformedURLException;
4 import java.net.URL;
5 import java.util.ArrayList;
6 import java.util.List;
7
8 public class RSpecScriptFactory extends AbstractScriptFactory {
9
10 public String getScript() throws MalformedURLException {
11 StringBuilder builder = new StringBuilder();
12
13 builder.append(getPrologScript());
14 builder.append(getClasspathElementsScript());
15 builder.append(getPluginClasspathScript());
16 builder.append(getConstantsConfigScript());
17 builder.append(getRSpecRunnerScript());
18 builder.append(getResultsScript());
19
20 return builder.toString();
21 }
22
23 private String getConstantsConfigScript() {
24 StringBuilder builder = new StringBuilder();
25
26 builder.append("BASE_DIR=%q(" + baseDir + ")\n");
27 builder.append("SPEC_DIR=%q(" + sourceDir + ")\n");
28 builder.append("REPORT_PATH=%q(" + reportPath + ")\n");
29 builder.append("$: << SPEC_DIR\n");
30
31 return builder.toString();
32 }
33
34 private String getRSpecRunnerScript() {
35 StringBuilder builder = new StringBuilder();
36
37 builder.append("require %q(rubygems)\n");
38 builder.append("require %q(spec)\n");
39 builder.append("require %q(org/codehaus/mojo/rspec/maven_progress_formatter)\n");
40 builder.append("options = ::Spec::Runner::OptionParser.parse([\n");
41 builder.append(" SPEC_DIR, '-f', \"html#{REPORT_PATH}\", '-f', 'MavenProgressFormatter', *ARGV\n");
42 builder.append("], STDERR, STDOUT)\n");
43 builder.append("::Spec::Runner::CommandLine.run(options)\n");
44
45 return builder.toString();
46 }
47
48 private String getResultsScript() {
49 StringBuilder builder = new StringBuilder();
50
51 builder.append("if File.new(REPORT_PATH, 'r').read =~ /, 0 failures/ \n");
52 builder.append(" false\n");
53 builder.append("else\n");
54 builder.append(" true\n");
55 builder.append("end\n");
56
57 return builder.toString();
58 }
59
60 private String getPrologScript() {
61 StringBuilder builder = new StringBuilder();
62
63 builder.append("require %(java)\n");
64
65 return builder.toString();
66 }
67
68 private String getClasspathElementsScript() throws MalformedURLException {
69 List<String> jars = new ArrayList<String>();
70 List<String> directories = new ArrayList<String>();
71
72 for (String path : classpathElements) {
73 if (path.endsWith(".jar")) {
74 jars.add(path);
75 } else {
76 directories.add(path);
77 }
78 }
79
80 StringBuilder script = new StringBuilder();
81
82 script.append("MOJO_CLASSPATH={\n");
83 script.append(" :directories=>[\n");
84 for (String item : directories) {
85 script.append(" %q(" + item + "),\n");
86 }
87 script.append(" ],\n");
88 script.append(" :jars=>[\n");
89 for (String item : jars) {
90 script.append(" %q(" + item + "),\n");
91 }
92 script.append(" ],\n");
93 script.append("}\n");
94
95 script.append("\n\n");
96
97 script.append("MOJO_CLASSPATH[:directories].each do |dir|\n");
98 script.append(" $: << dir\n");
99 script.append("end\n");
100
101 script.append("MOJO_CLASSPATH[:jars].each do |jar|\n");
102 script.append(" require jar\n");
103 script.append("end\n");
104
105 return script.toString();
106
107 }
108
109 private String getPluginClasspathScript() {
110
111 String pathToClass = getClass().getName().replaceAll("\\.", "/") + ".class";
112 URL here = getClass().getClassLoader().getResource(pathToClass);
113
114 String herePath = here.getPath();
115
116 if (herePath.startsWith("file:")) {
117 herePath = herePath.substring(5);
118 int bangLoc = herePath.indexOf("!");
119
120 if (bangLoc > 0) {
121 herePath = herePath.substring(0, bangLoc);
122 }
123 }
124
125 if (herePath.endsWith(".jar")) {
126 return "require %q(" + herePath + ")\n";
127 } else {
128 return "$: << %q(" + herePath + ")\n";
129 }
130 }
131
132 @Override
133 protected String getScriptName() {
134 return "rspec-runner.rb";
135 }
136
137 }