Skip to content

Commit

Permalink
Merge pull request google#4 from cgruber/package_refactor
Browse files Browse the repository at this point in the history
Refactor config objects and deal with the fallout
  • Loading branch information
cgruber authored Feb 19, 2019
2 parents cd4a83c + e9ea8c7 commit 665fc03
Show file tree
Hide file tree
Showing 113 changed files with 598 additions and 471 deletions.
2 changes: 2 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
build --javacopt=-Aautovaluegson.defaultCollectionsToEmpty=true
build --strategy=KotlinCompile=worker
build --nouse_ijars

Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import com.google.devtools.moe.client.directives.Directive;
import com.google.devtools.moe.client.directives.Directives;
import com.google.devtools.moe.client.options.OptionsParser;
import com.google.devtools.moe.client.project.InvalidProject;

import java.io.IOException;
import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package(default_visibility = ["//client:__subpackages__"])
CORE_SRCS = [
"FileSystem.java",
"Lifetimes.java",
"InvalidProject.java",
"MoeProblem.java",
"MoeUserProblem.java",
"NoopFileSystem.java",
Expand All @@ -31,6 +32,8 @@ java_library(
deps = [
":core",
"//client/src/main/java/com/google/devtools/moe/client/codebase/expressions",
"//client/src/main/java/com/google/devtools/moe/client/config",
"//client/src/main/java/com/google/devtools/moe/client/gson",
"//client/src/main/java/com/google/devtools/moe/client/qualifiers",
"@maven//args4j",
"@maven//com/google/auto/factory",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,24 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.devtools.moe.client.gson;
package com.google.devtools.moe.client;

import static com.google.gson.FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES;

import com.google.common.collect.ImmutableList;
import com.google.devtools.moe.client.MoeTypeAdapterFactory;
import com.google.devtools.moe.client.config.ConfigTypeAdapterFactory;
import com.google.devtools.moe.client.database.RepositoryEquivalence;
import com.google.devtools.moe.client.gson.ImmutableListDeserializer;
import com.google.devtools.moe.client.gson.JsonObjectDeserializer;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import dagger.Module;
import dagger.Provides;
import javax.inject.Singleton;

/**
* A Dagger module to provide and configure Gson
*/
/** A Dagger module to provide and configure Gson */
@Module
public final class GsonModule {
// TODO(user): eliminate this and make provideGson package private
Expand All @@ -37,6 +39,7 @@ public final class GsonModule {
new GsonBuilder()
.setPrettyPrinting()
.setFieldNamingPolicy(LOWER_CASE_WITH_UNDERSCORES)
.registerTypeAdapterFactory(ConfigTypeAdapterFactory.create())
.registerTypeAdapterFactory(MoeTypeAdapterFactory.create())
.registerTypeHierarchyAdapter(ImmutableList.class, new ImmutableListDeserializer())
.registerTypeHierarchyAdapter(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.google.devtools.moe.client.project;
package com.google.devtools.moe.client;

import com.google.common.base.Strings;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@
import com.google.devtools.moe.client.codebase.ExpressionModule;
import com.google.devtools.moe.client.database.FileDb;
import com.google.devtools.moe.client.directives.Directives;
import com.google.devtools.moe.client.gson.GsonModule;
import com.google.devtools.moe.client.options.OptionsModule;
import com.google.devtools.moe.client.project.FileReadingProjectContextFactory;
import com.google.devtools.moe.client.project.ProjectConfig;
import com.google.devtools.moe.client.config.ProjectConfig;
import com.google.devtools.moe.client.project.ProjectContext;
import com.google.devtools.moe.client.project.ProjectContext.NoopProjectContext;
import com.google.devtools.moe.client.project.ProjectContextFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.devtools.moe.client.gson;
package com.google.devtools.moe.client;

import com.google.gson.TypeAdapterFactory;
import com.ryanharter.auto.value.gson.GsonTypeAdapterFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@
*/
@AutoValue
public abstract class Codebase implements Keepable<Codebase> {
public abstract File path();
public abstract File root();

@Override
public Collection<Path> toKeep() {
return asList(path().toPath());
return asList(root().toPath());
}

public abstract String projectSpace();
Expand All @@ -57,8 +57,8 @@ public Collection<Path> toKeep() {
* @param expression an expression that generates this Codebase. This expression identifies the
* Codebase.
*/
public static Codebase create(File path, String projectSpace, Expression expression) {
return new AutoValue_Codebase(path, projectSpace, expression);
public static Codebase create(File root, String projectSpace, Expression expression) {
return new AutoValue_Codebase(root, projectSpace, expression);
}

@Override
Expand All @@ -73,19 +73,19 @@ public String toString() {
*/
@Override
public boolean equals(Object other) {
return other instanceof Codebase && path().equals(((Codebase) other).path());
return other instanceof Codebase && root().equals(((Codebase) other).root());
}

@Override
public int hashCode() {
return path().hashCode();
return root().hashCode();
}

/**
* @return the path of a file in this Codebase
*/
public File getFile(String relativeFilename) {
return new File(path(), relativeFilename);
return new File(root(), relativeFilename);
}

/**
Expand All @@ -109,7 +109,7 @@ public void checkProjectSpace(String projectSpace) {
* or translating by "imprinting" them with the EditExpression or TranslateExpression.
*/
public Codebase copyWithExpression(Expression newExpression) {
return Codebase.create(path(), projectSpace(), newExpression);
return Codebase.create(root(), projectSpace(), newExpression);
}

/**
Expand All @@ -118,6 +118,6 @@ public Codebase copyWithExpression(Expression newExpression) {
* space it was translated to.
*/
public Codebase copyWithProjectSpace(String newProjectSpace) {
return Codebase.create(path(), newProjectSpace, expression());
return Codebase.create(root(), newProjectSpace, expression());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public MergeResult merge(Codebase original, Codebase modified, Codebase destinat
}

private Set<String> findFiles(Codebase codebase) {
return makeFilenamesRelative(filesystem.findFiles(codebase.path()), codebase.path());
return makeFilenamesRelative(filesystem.findFiles(codebase.root()), codebase.root());
}

private boolean areDifferent(String filename, File x, File y) {
Expand Down Expand Up @@ -211,7 +211,7 @@ void generateMergedFile(
// Merges the changes that lead from origFile to modFile into mergedFile (which is a copy
// of destFile). After, mergedFile will have the combined changes of modFile and destFile.
cmd.runCommand(
resultBuilder.mergedCodebase().path().getAbsolutePath(),
resultBuilder.mergedCodebase().root().getAbsolutePath(),
"merge",
ImmutableList.of(
mergedFile.getAbsolutePath(), origFile.getAbsolutePath(), modFile.getAbsolutePath()));
Expand Down Expand Up @@ -248,7 +248,7 @@ public abstract static class MergeResult {

/** Print the results of a merge to the UI. */
public void report(Ui ui) {
ui.message("Merged codebase generated at: %s", mergedCodebase().path().getAbsolutePath());
ui.message("Merged codebase generated at: %s", mergedCodebase().root().getAbsolutePath());
if (failedFiles().isEmpty()) {
ui.message("%d files merged successfully. No merge conflicts.", mergedFiles().size());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public Codebase createCodebase(EditExpression expression, ProjectContext context

try (Task task =
ui.newTask(
"edit", "Editing %s with editor %s", codebaseToEdit.path(), editor.getDescription())) {
"edit", "Editing %s with editor %s", codebaseToEdit.root(), editor.getDescription())) {
return task.keep(
editor.edit(codebaseToEdit, expression.getOperation().getTerm().getOptions()))
.copyWithExpression(expression);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package com.google.devtools.moe.client.codebase;

import com.google.devtools.moe.client.FileSystem.Lifetime;
import com.google.devtools.moe.client.project.RepositoryConfig;
import com.google.devtools.moe.client.config.RepositoryConfig;
import java.io.File;
import javax.annotation.Nullable;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public Codebase createCodebase(TranslateExpression expression, ProjectContext co
ui.newTask(
"translate",
"Translating %s from project space \"%s\" to \"%s\"",
codebaseToTranslate.path(),
codebaseToTranslate.root(),
codebaseToTranslate.projectSpace(),
toProjectSpace)) {

Expand All @@ -56,7 +56,7 @@ public Codebase createCodebase(TranslateExpression expression, ProjectContext co
// Don't mark the translated codebase for persistence if it wasn't allocated by the
// Translator.
if (translatedCodebase.equals(codebaseToTranslate)) {
translateTask.result().append(translatedCodebase.path() + " (unmodified)");
translateTask.result().append(translatedCodebase.root() + " (unmodified)");
} else {
translateTask.keep(translatedCodebase);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package(default_visibility = ["//client:__subpackages__"])

java_library(
name = "config",
srcs = glob(["*.java", "*.kt"]),
javacopts = ["-Aautovaluegson.defaultCollectionsToEmpty=true"],
deps = [
"//client/src/main/java/com/google/devtools/moe/client:core",
"//client/src/main/java/com/google/devtools/moe/client/gson",
"@maven//com/google/auto/value",
"@maven//com/google/code/gson",
"@maven//com/google/guava",
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (c) 2015 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.devtools.moe.client.config;

import com.google.gson.TypeAdapterFactory;
import com.ryanharter.auto.value.gson.GsonTypeAdapterFactory;

@GsonTypeAdapterFactory
public abstract class ConfigTypeAdapterFactory implements TypeAdapterFactory {
public static TypeAdapterFactory create() {
return new AutoValueGson_ConfigTypeAdapterFactory();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
* limitations under the License.
*/

package com.google.devtools.moe.client.project;
package com.google.devtools.moe.client.config;

import com.google.auto.value.AutoValue;
import com.google.devtools.moe.client.translation.editors.Editor;
import com.google.devtools.moe.client.InvalidProject;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.TypeAdapter;
Expand All @@ -30,7 +30,7 @@
@AutoValue
public abstract class EditorConfig {
@Nullable
public abstract Editor.Type type();
public abstract EditorType type();

@Nullable
@SerializedName("scrubber_config") // TODO(cushon): remove pending rharter/auto-value-gson#18
Expand All @@ -47,12 +47,12 @@ public abstract class EditorConfig {
public abstract boolean useRegex();

// TODO(cgruber): Push validation around the whole structure.
void validate() throws InvalidProject {
public void validate() throws InvalidProject {
InvalidProject.assertNotNull(type(), "Missing type in editor");
}

public static EditorConfig create(
Editor.Type type,
EditorType type,
ScrubberConfig scrubberConfig,
String commandString,
JsonObject mappings,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.google.devtools.moe.client.config;

/**
* Enum of all known editor implementations.
*
* All values are valid JSON editor types.
*/
public enum EditorType {
identity,
scrubber,
patcher,
shell,
renamer
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.devtools.moe.client.repositories;
package com.google.devtools.moe.client.config;

import com.google.common.collect.ImmutableList;

Expand All @@ -30,7 +30,7 @@ public class MetadataScrubberConfig {

/**
* Formatting for changelog adapted from fromRepository for commits in toRepository. See
* {@link DescriptionMetadataScrubber}.
* {@link com.google.devtools.moe.client.repositories.DescriptionMetadataScrubber}.
*/
private String logFormat = "{description}\n\n\tChange on {date} by {author}\n";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@
* limitations under the License.
*/

package com.google.devtools.moe.client.migrations;
package com.google.devtools.moe.client.config;

import com.google.devtools.moe.client.gson.GsonUtil;
import com.google.devtools.moe.client.project.InvalidProject;
import com.google.devtools.moe.client.repositories.MetadataScrubberConfig;
import com.google.devtools.moe.client.InvalidProject;

/**
* Configuration for a MOE migration.
Expand Down
Loading

0 comments on commit 665fc03

Please sign in to comment.