diff --git a/README.md b/README.md index 283213f..ab01f63 100644 --- a/README.md +++ b/README.md @@ -347,7 +347,7 @@ add multiple resources with a relatively simple shell script. For example: If you have content already setup in Transifex, you may want to setup local resources in order to pull the language files on your system. In order to do -that, you can run: +that, you can run the following command with Linux or Mac OS: ```sh tx add remote \ @@ -355,6 +355,10 @@ tx add remote \ https://www.transifex.com/myorganization/myproject/dashboard/ ``` +> The use of tx add remote appends the content in the .tx/config file and does not overwrite it. However, if the project and resource exist in the .tx/config file, then it will overwrite the previous information for the specific project & resource. + +For Windows OS, please use double quotes instead of single quotes in the following example. + This will create entries in your configuration file for each resource in your remote project. ie the configuration file may look like this: @@ -509,6 +513,9 @@ This means that the _remote_ `pt_PT` language code maps to the _local_ `pt-pt` language code and the _remote_ `pt_BR` language code maps to the _local_ `pt-br` language code. +The REMOTE_CODE is the language code supported by Transifex. And the LOCAL_CODE is your +language code. + The `-l` flag works with both _local_ and _remote_ language codes. **Skipping pushing older files:** @@ -734,6 +741,12 @@ default to taking the filesystem timestamp into account. when using `-a/--all` flag and you don't want to change the existing files but only download other language files. +`--mode/-m`: The translation mode of the downloaded file. This can be one of the +following: `'default', 'reviewed'`, `'proofread'`, `'translator'`, `'untranslated'`, + `'onlytranslated'`, `'onlyreviewed'`, `'onlyproofread'`, `'sourceastranslation'` **(default + mode is: **`'default'`). Use like` 'tx pull -m proofread'` to download only proofread + translations. + - `--branch`: Using this flag, you can access copies of the regular remote resource that are tied to the provided branch. So if `tx pull proj.res` pulls from the `https://www.transifex.com/org/proj/res` resource, then `tx diff --git a/cmd/tx/main.go b/cmd/tx/main.go index 331f038..a39c6b5 100644 --- a/cmd/tx/main.go +++ b/cmd/tx/main.go @@ -437,6 +437,11 @@ func Main() { Aliases: []string{"d"}, Usage: "Whether skip existing files", }, + &cli.BoolFlag{ + Name: "keep-new-files", + Usage: "Used with --disable-ovewrite to create new files " + + "if file already exists with a '.new' extension.", + }, &cli.BoolFlag{ Name: "skip", Usage: "Whether to skip on errors", @@ -537,6 +542,7 @@ func Main() { Source: c.Bool("source"), Translations: c.Bool("translations"), DisableOverwrite: c.Bool("disable-overwrite"), + KeepNewFiles: c.Bool("keep-new-files"), All: c.Bool("all"), ResourceIds: resourceIds, UseGitTimestamps: c.Bool("use-git-timestamps"), diff --git a/internal/txlib/pull.go b/internal/txlib/pull.go index 3f23933..48e4717 100644 --- a/internal/txlib/pull.go +++ b/internal/txlib/pull.go @@ -26,6 +26,7 @@ type PullCommandArguments struct { Translations bool All bool DisableOverwrite bool + KeepNewFiles bool ResourceIds []string UseGitTimestamps bool Branch string @@ -378,8 +379,12 @@ func (task *FilePullTask) Run(send func(string), abort func()) { _, err := os.Stat(sourceFile) if err == nil && args.DisableOverwrite { - sendMessage("Disable Overwrite is enabled, skipping", false) - return + if !args.KeepNewFiles { + sendMessage("Disable overwrite enabled, skipping", false) + return + } else { + sourceFile = sourceFile + ".new" + } } if !args.Force { @@ -447,8 +452,12 @@ func (task *FilePullTask) Run(send func(string), abort func()) { if filePath != "" { // Remote language file exists and so does local if args.DisableOverwrite { - sendMessage("Disable overwrite enabled, skipping", false) - return + if !args.KeepNewFiles { + sendMessage("Disable overwrite enabled, skipping", false) + return + } else { + filePath = filePath + ".new" + } } } else { // Remote language file exists but local does not diff --git a/internal/txlib/pull_test.go b/internal/txlib/pull_test.go index 123ca35..6871ec6 100644 --- a/internal/txlib/pull_test.go +++ b/internal/txlib/pull_test.go @@ -2,6 +2,7 @@ package txlib import ( "fmt" + "io/ioutil" "os" "strings" "testing" @@ -591,6 +592,149 @@ func TestDownloadPseudoTranslations(t *testing.T) { testSimpleGet(t, mockData, sourceDownloadUrl) } +func TestKeepNewFilesSource(t *testing.T) { + afterTest := beforeTest(t, nil, nil) + defer afterTest() + + cfg := getStandardConfig() + assertFileContent(t, "aaa.json", "") + + ts := getNewTestServer("New source") + defer ts.Close() + + mockData := jsonapi.MockData{ + resourceUrl: getResourceEndpoint(), + projectUrl: getProjectEndpoint(), + statsUrlSourceLanguage: getStatsEndpointSourceLanguage(), + sourceDownloadsUrl: getSourceDownloadsEndpoint(), + sourceDownloadUrl: getDownloadEndpoint(ts.URL), + } + + api := jsonapi.GetTestConnection(mockData) + err := PullCommand( + cfg, + &api, + &PullCommandArguments{ + FileType: "default", + Mode: "default", + Force: true, + Source: true, + ResourceIds: nil, + MinimumPercentage: -1, + Workers: 1, + KeepNewFiles: true, + DisableOverwrite: true, + }, + ) + if err != nil { + t.Errorf("%s", err) + } + + testSimpleGet(t, mockData, resourceUrl) + testSimpleGet(t, mockData, projectUrl) + testSimpleGet(t, mockData, statsUrlSourceLanguage) + testSimpleSourceDownload(t, mockData, "false") + testSimpleGet(t, mockData, sourceDownloadUrl) + + assertFileContent(t, "aaa.json.new", "New source") + assertFileContent(t, "aaa.json", "") +} + +func TestKeepNewFilesSourceOnlyWithDisableOverride(t *testing.T) { + afterTest := beforeTest(t, nil, nil) + defer afterTest() + + cfg := getStandardConfig() + assertFileContent(t, "aaa.json", "") + + ts := getNewTestServer("New source") + defer ts.Close() + + mockData := jsonapi.MockData{ + resourceUrl: getResourceEndpoint(), + projectUrl: getProjectEndpoint(), + statsUrlSourceLanguage: getStatsEndpointSourceLanguage(), + sourceDownloadsUrl: getSourceDownloadsEndpoint(), + sourceDownloadUrl: getDownloadEndpoint(ts.URL), + } + + api := jsonapi.GetTestConnection(mockData) + err := PullCommand( + cfg, + &api, + &PullCommandArguments{ + FileType: "default", + Mode: "default", + Force: true, + Source: true, + ResourceIds: nil, + MinimumPercentage: -1, + Workers: 1, + KeepNewFiles: true, + }, + ) + if err != nil { + t.Errorf("%s", err) + } + + testSimpleGet(t, mockData, resourceUrl) + testSimpleGet(t, mockData, projectUrl) + testSimpleGet(t, mockData, statsUrlSourceLanguage) + testSimpleSourceDownload(t, mockData, "false") + testSimpleGet(t, mockData, sourceDownloadUrl) + + assertFileContent(t, "aaa.json", "New source") + _, err = ioutil.ReadFile("aaa.json.new") + if err == nil { + t.Error("File not exist because DisableOverwrite is not true") + } +} + +func TestKeepNewFilesTranslation(t *testing.T) { + afterTest := beforeTest(t, []string{"el"}, nil) + defer afterTest() + + cfg := getStandardConfig() + + ts := getNewTestServer("This is the content") + defer ts.Close() + + mockData := jsonapi.MockData{ + resourceUrl: getResourceEndpoint(), + projectUrl: getProjectEndpoint(), + statsUrlAllLanguages: getStatsEndpointAllLanguages(), + translationDownloadsUrl: getTranslationDownloadsEndpoint(), + translationDownloadUrl: getDownloadEndpoint(ts.URL), + } + + api := jsonapi.GetTestConnection(mockData) + + arguments := PullCommandArguments{ + FileType: "default", + Mode: "default", + Force: true, + All: true, + ResourceIds: nil, + MinimumPercentage: -1, + Workers: 1, + KeepNewFiles: true, + DisableOverwrite: true, + } + + err := PullCommand(cfg, &api, &arguments) + if err != nil { + t.Errorf("%s", err) + } + + testSimpleGet(t, mockData, resourceUrl) + testSimpleGet(t, mockData, projectUrl) + testSimpleGet(t, mockData, statsUrlAllLanguages) + testSimpleTranslationDownload(t, mockData, "false") + testSimpleGet(t, mockData, translationDownloadUrl) + assertFileContent(t, "aaa-el.json", `{"hello": "world"}`) + assertFileContent(t, "aaa-el.json.new", "This is the content") +} + func assertFileContent(t *testing.T, expectedPath, expectedContent string) { data, err := os.ReadFile(expectedPath) if err != nil {