Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
rhbz1110627,rhbz1038852 - add src dir and trans dir to zanata config
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Huang committed Jun 30, 2014
1 parent 3c0bba5 commit dda35c9
Show file tree
Hide file tree
Showing 9 changed files with 217 additions and 60 deletions.
Expand Up @@ -45,8 +45,6 @@ public abstract class AbstractPushPullOptionsImpl<O extends PushPullOptions>

protected String[] locales;
private LocaleList effectiveLocales;
private File transDir;
private File srcDir;
private String fromDoc = DEF_FROM_DOC;
private boolean dryRun = DEFAULT_DRY_RUN;

Expand All @@ -67,43 +65,11 @@ public LocaleList getLocaleMapList() {
return effectiveLocales;
}

@Override
public File getSrcDir() {
return srcDir;
}

@Option(
aliases = { "-s" },
name = "--src-dir",
metaVar = "DIR",
required = true,
usage = "Base directory for source files (eg \".\", \"pot\", \"src/main/resources\")")
public
void setSrcDir(File file) {
this.srcDir = file;
}

@Override
public String getSrcDirParameterName() {
return "--src-dir";
}

@Override
public File getTransDir() {
return transDir;
}

@Option(
aliases = { "-t" },
name = "--trans-dir",
metaVar = "DIR",
required = true,
usage = "Base directory for translated files (eg \".\", \"po\", \"src/main/resources\")")
public
void setTransDir(File transDir) {
this.transDir = transDir;
}

@Override
public String getFromDoc() {
return fromDoc;
Expand Down
Expand Up @@ -53,4 +53,24 @@ public interface ConfigurableProjectOptions extends ConfigurableOptions {
public LocaleList getLocaleMapList();

public void setLocaleMapList(LocaleList locales);

@Option(
aliases = { "-s" },
name = "--src-dir",
metaVar = "DIR",
required = true,
usage = "Base directory for source files (eg \".\", \"pot\", \"src/main/resources\")")
void setSrcDir(File srcDir);

File getSrcDir();

@Option(
aliases = { "-t" },
name = "--trans-dir",
metaVar = "DIR",
required = true,
usage = "Base directory for translated files (eg \".\", \"po\", \"src/main/resources\")")
void setTransDir(File transDir);

File getTransDir();
}
Expand Up @@ -47,6 +47,8 @@ public abstract class ConfigurableProjectOptionsImpl extends
private String projectVersion;
private String projectType;
private LocaleList locales;
private File transDir;
private File srcDir;

@Override
public String getProj() {
Expand Down Expand Up @@ -124,4 +126,33 @@ public void setLocaleMapList(LocaleList locales) {
this.locales = locales;
}

@Option(
aliases = { "-s" },
name = "--src-dir",
metaVar = "DIR",
usage = "Base directory for source files (eg \".\", \"pot\", \"src/main/resources\")")
@Override
public void setSrcDir(File srcDir) {
this.srcDir = srcDir;
}

@Override
public File getSrcDir() {
return srcDir;
}

@Option(
aliases = { "-t" },
name = "--trans-dir",
metaVar = "DIR",
usage = "Base directory for translated files (eg \".\", \"po\", \"src/main/resources\")")
@Override
public void setTransDir(File transDir) {
this.transDir = transDir;
}

@Override
public File getTransDir() {
return transDir;
}
}
Expand Up @@ -18,6 +18,8 @@
import org.zanata.client.exceptions.ConfigException;
import org.zanata.rest.client.ZanataProxyFactory;
import org.zanata.util.VersionUtility;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Strings;

public class OptionsUtil {
private static final Logger log = LoggerFactory
Expand Down Expand Up @@ -88,6 +90,7 @@ private static void applyProjectConfig(ConfigurableProjectOptions opts,
if (opts.getProjectType() == null) {
opts.setProjectType(config.getProjectType());
}
applySrcDirAndTransDirFromProjectConfig(opts, config);
LocaleList locales = config.getLocales();
opts.setLocaleMapList(locales);

Expand All @@ -96,6 +99,47 @@ private static void applyProjectConfig(ConfigurableProjectOptions opts,
}
}

/**
* Note: command line options take precedence over pom.xml which
* takes precedence over zanata.xml.
*
* @param opts
* options
* @param config
* config from project configuration file i.e. zanata.xml
*/
@VisibleForTesting
protected static void applySrcDirAndTransDirFromProjectConfig(
ConfigurableProjectOptions opts, ZanataConfig config) {
// apply srcDir configuration
boolean srcDirInOption = opts.getSrcDir() != null;
boolean srcDirInConfig = !Strings.isNullOrEmpty(config.getSrcDir());
if (!srcDirInOption && srcDirInConfig) {
opts.setSrcDir(new File(config.getSrcDir()));
} else if (srcDirInOption && !srcDirInConfig) {
// option is provided from commandline or in pom.xml
log.info("You can define your source directory in zanata.xml now (<src-dir>{}</src-dir>)", opts.getSrcDir());
} else if (srcDirInOption && !config.getSrcDir().equals(opts.getSrcDir().getPath())) {
// option mismatch between zanata.xml and given value
log.warn("Source directory in zanata.xml is set to [{}] but is now given as [{}]", config.getSrcDir(), opts.getSrcDir());
log.warn("You are overriding your source directory defined in zanata.xml");
}

// apply transDir configuration
boolean transDirInOption = opts.getTransDir() != null;
boolean transDirInConfig = !Strings.isNullOrEmpty(config.getTransDir());
if (!transDirInOption && transDirInConfig) {
opts.setTransDir(new File(config.getTransDir()));
} else if (transDirInOption && !transDirInConfig) {
// option is provided from commandline or in pom.xml
log.info("You can define your translation directory in zanata.xml now (<trans-dir>{}</trans-dir>)", opts.getTransDir());
} else if (transDirInOption && !config.getTransDir().equals(opts.getTransDir().getPath())) {
// option mismatch between zanata.xml and given value
log.warn("Translation directory in zanata.xml is set to [{}] but is now given as [{}]", config.getTransDir(), opts.getTransDir());
log.warn("You are overriding your translation directory defined in zanata.xml");
}
}

/**
* Applies values from the user's personal configuration unless they have
* been set directly (by parameters or by project configuration).
Expand Down
Expand Up @@ -26,7 +26,7 @@ public String getCommandName() {

@Override
public String getCommandDescription() {
return "Initialize Zanata project structure";
return "Initialize Zanata project configuration";
}

@Override
Expand Down
Expand Up @@ -38,7 +38,7 @@
*
*/
@XmlType(name = "configType", propOrder = { "url", "project", "projectVersion",
"projectType", "hooks", "locales" })
"projectType", "srcDir", "transDir", "hooks", "locales" })
@XmlRootElement(name = "config")
public class ZanataConfig implements Serializable {
private static final long serialVersionUID = 1L;
Expand All @@ -47,6 +47,9 @@ public class ZanataConfig implements Serializable {
private URL url;
private String projectType;
private String projectVersion;
// default to current directory
private String srcDir = ".";
private String transDir = ".";
private List<CommandHook> hooks = new ArrayList<CommandHook>();

public ZanataConfig() {
Expand Down Expand Up @@ -108,4 +111,21 @@ public void setProjectVersion(String version) {
this.projectVersion = version;
}

@XmlElement(name = "src-dir")
public String getSrcDir() {
return srcDir;
}

public void setSrcDir(String srcDir) {
this.srcDir = srcDir;
}

@XmlElement(name = "trans-dir")
public String getTransDir() {
return transDir;
}

public void setTransDir(String transDir) {
this.transDir = transDir;
}
}
@@ -0,0 +1,68 @@
package org.zanata.client.commands;

import java.io.File;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.zanata.client.config.ZanataConfig;

import static org.mockito.Matchers.any;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class OptionsUtilTest {

@Mock
private ConfigurableProjectOptions opts;
private ZanataConfig config;

@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
config = new ZanataConfig();
}

@Test
public void willApplyConfigFromFileIfNotSetInOptions() throws Exception {
// Given: options are not set and exists in zanata config
when(opts.getSrcDir()).thenReturn(null);
when(opts.getTransDir()).thenReturn(null);
config.setSrcDir("a");
config.setTransDir("b");

// When:
OptionsUtil.applySrcDirAndTransDirFromProjectConfig(opts, config);

// Then:
verify(opts).setSrcDir(new File("a"));
verify(opts).setTransDir(new File("b"));
}
@Test
public void willSetToDefaultValueIfNeitherHasValue() {
when(opts.getSrcDir()).thenReturn(null);
when(opts.getTransDir()).thenReturn(null);

OptionsUtil.applySrcDirAndTransDirFromProjectConfig(opts, config);

verify(opts).setSrcDir(new File("."));
verify(opts).setTransDir(new File("."));
}
@Test
public void optionTakesPrecedenceOverConfig() {
// Given: options are set and exists in zanata config
when(opts.getSrcDir()).thenReturn(new File("pot"));
when(opts.getTransDir()).thenReturn(new File("."));
config.setSrcDir("a");
config.setTransDir("b");

// When:
OptionsUtil.applySrcDirAndTransDirFromProjectConfig(opts, config);

// Then:
verify(opts, never()).setSrcDir(any(File.class));
verify(opts, never()).setTransDir(any(File.class));
}
}
Expand Up @@ -117,20 +117,6 @@ private String toMavenModuleID(MavenProject module) {
@SuppressWarnings("MismatchedQueryAndUpdateOfCollection")
private List<MavenProject> reactorProjects;

/**
* Base directory for source-language files
*
* @parameter expression="${zanata.srcDir}" default-value="."
*/
private File srcDir;

/**
* Base directory for target-language files (translations)
*
* @parameter expression="${zanata.transDir}" default-value="."
*/
private File transDir;

/**
* Specifies a document from which to begin the push operation. Documents
* before this document (sorted alphabetically) will not be pushed.
Expand Down Expand Up @@ -167,21 +153,11 @@ public boolean isDryRun() {
return dryRun;
}

@Override
public File getSrcDir() {
return srcDir;
}

@Override
public String getSrcDirParameterName() {
return "srcDir";
}

@Override
public File getTransDir() {
return transDir;
}

@Override
public String getFromDoc() {
return fromDoc;
Expand Down
Expand Up @@ -61,6 +61,19 @@ public abstract class ConfigurableProjectMojo<O extends ConfigurableOptions>

private LocaleList localeMapList;

/**
* Base directory for source-language files
*
* @parameter expression="${zanata.srcDir}"
*/
private File srcDir;
/**
* Base directory for target-language files (translations)
*
* @parameter expression="${zanata.transDir}"
*/
private File transDir;

public ConfigurableProjectMojo() {
super();
}
Expand Down Expand Up @@ -105,6 +118,16 @@ public void setProjectType(String projectType) {
this.projectType = projectType;
}

@Override
public File getSrcDir() {
return srcDir;
}

@Override
public File getTransDir() {
return transDir;
}

@Override
public LocaleList getLocaleMapList() {
return localeMapList;
Expand All @@ -115,4 +138,13 @@ public void setLocaleMapList(LocaleList localeMapList) {
this.localeMapList = localeMapList;
}

@Override
public void setSrcDir(File srcDir) {
this.srcDir = srcDir;
}

@Override
public void setTransDir(File transDir) {
this.transDir = transDir;
}
}

0 comments on commit dda35c9

Please sign in to comment.