<commit_before># Copyright 2017 <NAME>
# 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.

# Author: <NAME> <<EMAIL>>
# date  : 2017-07-08

import os, osproc, ospaths
import subexes, pegs
import parseopt2, strutils

import model/build
import model/package, model/reason
import publisher, git
import fetcher, resolver
import compilehelper
import logger

type
  NimNotFoundError = object of Exception
  ArgumentError = object of ValueError

proc getOutputHeader(nimDirPath, name, version: string): string =
  result = "---\n"
  result &= getNimVersion(nimDirPath) & "\n"
  result &= subex("$# v$#\n") % [name, version]
  result &= "---\n\n"

proc nims(basePath: string): seq[string] =
  result = @[]
  for n in walkDirs(basePath / "*"):
    result.add n

proc compileOutputs(res, command: string): string =
  result = "+ " & command & "\n"
  result &= res & "\n"

proc compileNimFiles(build: var Build, srcPath, nimDirPath: string): string =
  result = ""
  for path in walkFiles(srcPath / "*.nim"):
    let
      command = getBuildCommand(
        nimDirPath.getNimBinPath(), path, getDependenciesPath())
      (res, code) = execCmdEx command
    log(command, res)
    if code == 0:
      build.succeeded()
    else:
      build.failed()
    result &= compileOutputs(res, command)

proc fetchAll(publisher: Publisher, basePath, version: string) =
  let nims = basePath.nims()
  for nim in nims:
    if not nim.existsNimCommands():
      raise newException(NimNotFoundError, "h")

  var jobs: seq[Job] = @[]
  for fetchResult in fetch(nims[0]):
    let
      info = fetchResult.packageInfo
      name = info.name

    var job = newJob(name, info.version)

    for nim in nims:
      var build = newBuild(nim.pathToVersion())

      job.message = getOutputHeader(nim, name, info.version)

      if fetchResult.installResultCode != 0:
        job.message &= fetchResult.installResult
        build.reason = Reason.installFailed
      else:
        job.message &= build.compileNimFiles(info.getResolveSrcPath(), nim)

        if build.empty():
          job.message &= "nim script not found."

        if build.allGreen():
          build.reason = Reason.success
        else:
          build.reason = Reason.compileFailed

      job.builds.add build
      publisher.addBuildResult(name, job.message)

    jobs.add job
    removeTempFiles()
  publisher.addResults jobs, nims
  publisher.commit()

proc getExpandPath(path: string): string =
  result = path.replace("~", getEnv("HOME"))

proc existsGitProject(path: string): bool =
  result = (path / ".git").existsDir()

when isMainModule:
  var
    basePath: string
    version: string
    projectPath: string = ""
  for kind, key, val in getopt():
    case kind
    of cmdArgument:
      basePath = key
    of cmdLongOption, cmdShortOption:
      case key
      of "v", "version":
        version = val
      of "log-project":
        projectPath = val
      else:
        discard
    else:
      discard

  let
    expandedBasePath = basePath.getExpandPath()
    expandedProjectPath = projectPath.getExpandPath()

  if not expandedProjectPath.existsGitProject():
    raise newException(ArgumentError, "h")

  let pub = Publisher(basePath: expandedProjectPath)
  fetchAll(pub, expandedBasePath, version)<commit_msg>fix error message<commit_after># Copyright 2017 <NAME>aka
# 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.

# Author: <NAME> <<EMAIL>>
# date  : 2017-07-08

import os, osproc, ospaths
import subexes, pegs
import parseopt2, strutils

import model/build
import model/package, model/reason
import publisher, git
import fetcher, resolver
import compilehelper
import logger

type
  NimNotFoundError = object of Exception
  ArgumentError = object of ValueError

proc getOutputHeader(nimDirPath, name, version: string): string =
  result = "---\n"
  result &= getNimVersion(nimDirPath) & "\n"
  result &= subex("$# v$#\n") % [name, version]
  result &= "---\n\n"

proc nims(basePath: string): seq[string] =
  result = @[]
  for n in walkDirs(basePath / "*"):
    result.add n

proc compileOutputs(res, command: string): string =
  result = "+ " & command & "\n"
  result &= res & "\n"

proc compileNimFiles(build: var Build, srcPath, nimDirPath: string): string =
  result = ""
  for path in walkFiles(srcPath / "*.nim"):
    let
      command = getBuildCommand(
        nimDirPath.getNimBinPath(), path, getDependenciesPath())
      (res, code) = execCmdEx command
    log(command, res)
    if code == 0:
      build.succeeded()
    else:
      build.failed()
    result &= compileOutputs(res, command)

proc fetchAll(publisher: Publisher, basePath, version: string) =
  let nims = basePath.nims()
  for nim in nims:
    if not nim.existsNimCommands():
      raise newException(NimNotFoundError,
        subex("nim or nimble not found in $#.") % [nim])

  var jobs: seq[Job] = @[]
  for fetchResult in fetch(nims[0]):
    let
      info = fetchResult.packageInfo
      name = info.name

    var job = newJob(name, info.version)

    for nim in nims:
      var build = newBuild(nim.pathToVersion())

      job.message = getOutputHeader(nim, name, info.version)

      if fetchResult.installResultCode != 0:
        job.message &= fetchResult.installResult
        build.reason = Reason.installFailed
      else:
        job.message &= build.compileNimFiles(info.getResolveSrcPath(), nim)

        if build.empty():
          job.message &= "nim script not found."

        if build.allGreen():
          build.reason = Reason.success
        else:
          build.reason = Reason.compileFailed

      job.builds.add build
      publisher.addBuildResult(name, job.message)

    jobs.add job
    removeTempFiles()
  publisher.addResults jobs, nims
  publisher.commit()

proc getExpandPath(path: string): string =
  result = path.replace("~", getEnv("HOME"))

proc existsGitProject(path: string): bool =
  result = (path / ".git").existsDir()

when isMainModule:
  var
    basePath: string
    version: string
    projectPath: string = ""
  for kind, key, val in getopt():
    case kind
    of cmdArgument:
      basePath = key
    of cmdLongOption, cmdShortOption:
      case key
      of "v", "version":
        version = val
      of "log-project":
        projectPath = val
      else:
        discard
    else:
      discard

  let
    expandedBasePath = basePath.getExpandPath()
    expandedProjectPath = projectPath.getExpandPath()

  if not expandedProjectPath.existsGitProject():
    raise newException(ArgumentError,
      """Need to set git project to "--log-project" option.""")

  let pub = Publisher(basePath: expandedProjectPath)
  fetchAll(pub, expandedBasePath, version)
{'fraction_non_alphanumeric': 0.10763980047256498, 'fraction_numerical': 0.005250721974271462, 'mean_word_length': 3.202426916712631, 'pattern_counts': {'":': 4, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5641908', 'n_tokens_mistral': 2326, 'n_tokens_neox': 2272, 'n_words': 712}