Skip to content

Commit

Permalink
feat: export from tajriba.json file (#544)
Browse files Browse the repository at this point in the history
  • Loading branch information
npaton committed Apr 11, 2024
1 parent 5467a49 commit efc0fd5
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 139 deletions.
17 changes: 17 additions & 0 deletions .changeset/export-from-file.md
@@ -0,0 +1,17 @@
---
"@empirica/core": minor
---

Export now supports passing a tajriba.json file as the first argument to export
the data directly from a file instead of automatically detecting the file from
the current project.

```sh
# At the root of the project.
empirica export

# Anywhere, although it uses the global version of empirica (not the version
# locked in your project). Upgrade to the latest version of empirica with:
# `empirica upgrade --global`
empirica export path/to/tajriba.json
```
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -2,6 +2,7 @@
*.db
*.tgz
*.tar.zst
*.zip
tajriba.json
internal/vendored/transport
yarn-error.log
Expand Down
165 changes: 26 additions & 139 deletions cmds/empirica/cmd/export.go
Expand Up @@ -2,28 +2,36 @@ package cmd

import (
"fmt"
"net/url"
"os"
"os/exec"
"path"
"strings"
"time"

"github.com/empiricaly/empirica/internal/build"
"github.com/empiricaly/empirica/internal/experiment"
"github.com/empiricaly/empirica/internal/export"
"github.com/empiricaly/empirica/internal/settings"
"github.com/empiricaly/empirica/internal/templates"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)

func addExportCommand(parent *cobra.Command) error {
cmd := &cobra.Command{
Use: "export",
Use: "export [tajriba.json file]",
Short: "Export empirica data",
// Long: ``,
Long: `export empirica data to a zip file containing CSV files with the data.
If ran without arguments, at the root of an experiment, it will use the data
file (.empirica/local/tajriba.json) of the current experiment.
You can also specify the path to the tajriba.json file to export as the first
argument. Note: it will use the global version of empirica (not the version
locked in your project). Upgrade to the latest version of empirica with:
empirica upgrade --global
The output file can be specified with the --out flag. If not specified, it will
be saved in the current working directory with the name:
<experiment-name or empirica>-<timestamp>.zip.
`,
SilenceUsage: true,
SilenceErrors: true,
Args: cobra.MaximumNArgs(1),
Expand All @@ -41,19 +49,23 @@ func addExportCommand(parent *cobra.Command) error {
return errors.Wrap(err, "parse out flag")
}

// fmt.Println("Setting up export environment...")
wd, err := os.Getwd()
if err != nil {
return errors.Wrap(err, "get working directory")
}

localDir := path.Join(wd, settings.EmpiricaDir, settings.LocalDir)
var tajfile string
if len(args) == 1 {
tajfile = args[0]
} else {
localDir := path.Join(wd, settings.EmpiricaDir, settings.LocalDir)

if _, err := os.Stat(localDir); err != nil {
return errors.New("no .empirica folder found, export must run within a project folder")
}
if _, err := os.Stat(localDir); err != nil {
return errors.New("no .empirica folder found, export must run within a project folder")
}

tajfile := path.Join(localDir, "tajriba.json")
tajfile = path.Join(localDir, "tajriba.json")
}

if _, err := os.Stat(tajfile); err != nil {
return errors.New("no tajriba.json file found, export must run within a project folder")
Expand All @@ -79,131 +91,6 @@ func addExportCommand(parent *cobra.Command) error {
}

return nil

exportScriptDir := path.Join(localDir, "export")

resolvedVersion := "not found"

vers, _, err := build.GetProjectRelease()
if err == nil {
resolvedVersion = vers.Version
} else {
serverDir := path.Join(wd, "server")
versServer := experiment.GetVersion(serverDir, build.EmpiricaPackageName)

if versServer != nil {
resolvedVersion = versServer.Resolved
} else {
buildVersion := build.Version()
if buildVersion != "" && strings.HasPrefix(buildVersion, "v") {
resolvedVersion = strings.TrimPrefix(buildVersion, "v")
}
}
}

// (re-)create export dir. We always reexport for simplicity, so we
// don't have to manage versions... Should optimize later.
if _, err := os.Stat(exportScriptDir); err == nil {
if resolvedVersion == "" {
os.RemoveAll(exportScriptDir)
} else {
// Check if version is identical to server/package.json
versExport := experiment.GetVersion(exportScriptDir, build.EmpiricaPackageName)
if versExport.Resolved != resolvedVersion {
os.RemoveAll(exportScriptDir)
}
}
}

empiricaCmd := "empirica"

if os.Getenv("EMPIRICA_DEV") != "" {
resolvedVersion = "link"

empiricaCmd = os.Args[0]

log.Warn().
Str("package", build.EmpiricaPackageName).
Str("EMPIRICA_DEV", "true").
Msg("export: using locally linked package")
} else {
if resolvedVersion == "not found" {
resolvedVersion = "latest"
}
}

fmt.Println("Exporting data...", empiricaCmd)

if _, err := os.Stat(exportScriptDir); err != nil {
if err := templates.CopyDir("", exportScriptDir, "export"); err != nil {
return errors.Wrap(err, "export: copy export script")
}

if resolvedVersion == "link" {
if err := experiment.RunCmd(ctx, exportScriptDir, empiricaCmd, "npm", "link", "@empirica/core"); err != nil {
return errors.Wrap(err, "server")
}
} else {
if err := experiment.RunCmdSilent(ctx, exportScriptDir, empiricaCmd, "npm", "install", "--silent"); err != nil {
return errors.Wrap(err, "server")
}

if err := experiment.RunCmdSilent(ctx, exportScriptDir, empiricaCmd, "npm", "install", "--silent", "-E", "@empirica/core@"+resolvedVersion); err != nil {
return errors.Wrap(err, "upgrade client")
}
}
}

// experimentName := conf.Name
// if experimentName == "" {
// experimentName = "empirica"
// }

// filename := out
// if out == "" {
// filename = path.Join(wd, fmt.Sprintf("%s-%s.zip", experimentName, time.Now().Format("2006-01-02-15-04-05")))
// }

exportArgs := []string{
"npm",
"run",
"export",
"--",
"--filename",
filename,
}

srtoken := conf.Tajriba.Auth.ServiceRegistrationToken

if len(args) == 0 {
tajfile := path.Join(localDir, "tajriba.json")
exportArgs = append(exportArgs, "--tajfile", tajfile)
} else {
if _, err := url.ParseRequestURI(args[0]); err != nil {
tajfile := path.Join(localDir, args[0])
exportArgs = append(exportArgs, "--tajfile", tajfile)
} else {
exportArgs = append(exportArgs, "--url", args[0])
}
}

exportArgs = append(exportArgs, "--srtoken", srtoken)

log.Info().
Str("args", strings.Join(exportArgs, " ")).
Msg("exporting data")

c := exec.CommandContext(ctx, empiricaCmd, exportArgs...)

c.Stderr = os.Stderr
c.Stdout = os.Stdout
c.Dir = exportScriptDir

if err := c.Run(); err != nil {
return errors.Wrap(err, "run export script")
}

return nil
},
}

Expand Down

0 comments on commit efc0fd5

Please sign in to comment.