<commit_before>/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 target

import (
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"
	"sort"
	"strings"

	"mynewt.apache.org/newt/newt/cli"
	"mynewt.apache.org/newt/newt/pkg"
	"mynewt.apache.org/newt/newt/project"
	"mynewt.apache.org/newt/util"
	"mynewt.apache.org/newt/yaml"
)

const TARGET_FILE_NAME string = "target.yml"

var globalTargetMap map[string]*Target

var targetSearchDirs = []string{
	"targets",
}

type Target struct {
	basePkg *pkg.LocalPackage

	// XXX: Probably don't need the below four fields; they can just be
	// retrieved from the viper object.  Keep them here for now for easy
	// initializiation of dummy targets.
	CompilerName string
	BspName      string
	AppName      string
	Arch         string
	BuildProfile string

	// target.yml configuration structure
	Vars map[string]string
}

func NewTarget(basePkg *pkg.LocalPackage) *Target {
	target := &Target{}
	target.Init(basePkg)
	return target
}

func LoadTarget(basePkg *pkg.LocalPackage) (*Target, error) {
	target := NewTarget(basePkg)
	if err := target.Load(basePkg); err != nil {
		return nil, err
	}

	return target, nil
}

func (target *Target) Init(basePkg *pkg.LocalPackage) {
	target.basePkg = basePkg
}

func (target *Target) Load(basePkg *pkg.LocalPackage) error {
	v, err := util.ReadConfig(basePkg.BasePath(),
		strings.TrimSuffix(TARGET_FILE_NAME, ".yml"))
	if err != nil {
		return err
	}

	target.Vars = map[string]string{}

	settings := v.AllSettings()
	for k, v := range settings {
		target.Vars[k] = v.(string)
	}

	target.CompilerName = target.Vars["target.compiler"]
	target.BspName = target.Vars["target.bsp"]
	target.AppName = target.Vars["target.app"]
	target.Arch = target.Vars["target.arch"]
	target.BuildProfile = target.Vars["target.build_profile"]

	// XXX: Verify required fields set?

	return nil
}

func (target *Target) Package() *pkg.LocalPackage {
	return target.basePkg
}

func (target *Target) Name() string {
	return target.basePkg.Name()
}

func (target *Target) ShortName() string {
	return filepath.Base(target.Name())
}

func (target *Target) App() *pkg.LocalPackage {
	dep, err := pkg.NewDependency(nil, target.AppName)
	if err != nil {
		fmt.Println("app name = %s\n", target.AppName)
		fmt.Println("dep is nil")
		return nil
	}

	appPkg := project.GetProject().ResolveDependency(dep)
	if appPkg == nil {
		fmt.Printf("app name = %s\n", target.AppName)
		return nil
	}

	return appPkg.(*pkg.LocalPackage)
}<commit_msg>Remove some debug printf calls.<commit_after>/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 target

import (
	"io/ioutil"
	"os"
	"path/filepath"
	"sort"
	"strings"

	"mynewt.apache.org/newt/newt/cli"
	"mynewt.apache.org/newt/newt/pkg"
	"mynewt.apache.org/newt/newt/project"
	"mynewt.apache.org/newt/util"
	"mynewt.apache.org/newt/yaml"
)

const TARGET_FILE_NAME string = "target.yml"

var globalTargetMap map[string]*Target

var targetSearchDirs = []string{
	"targets",
}

type Target struct {
	basePkg *pkg.LocalPackage

	// XXX: Probably don't need the below four fields; they can just be
	// retrieved from the viper object.  Keep them here for now for easy
	// initializiation of dummy targets.
	CompilerName string
	BspName      string
	AppName      string
	Arch         string
	BuildProfile string

	// target.yml configuration structure
	Vars map[string]string
}

func NewTarget(basePkg *pkg.LocalPackage) *Target {
	target := &Target{}
	target.Init(basePkg)
	return target
}

func LoadTarget(basePkg *pkg.LocalPackage) (*Target, error) {
	target := NewTarget(basePkg)
	if err := target.Load(basePkg); err != nil {
		return nil, err
	}

	return target, nil
}

func (target *Target) Init(basePkg *pkg.LocalPackage) {
	target.basePkg = basePkg
}

func (target *Target) Load(basePkg *pkg.LocalPackage) error {
	v, err := util.ReadConfig(basePkg.BasePath(),
		strings.TrimSuffix(TARGET_FILE_NAME, ".yml"))
	if err != nil {
		return err
	}

	target.Vars = map[string]string{}

	settings := v.AllSettings()
	for k, v := range settings {
		target.Vars[k] = v.(string)
	}

	target.CompilerName = target.Vars["target.compiler"]
	target.BspName = target.Vars["target.bsp"]
	target.AppName = target.Vars["target.app"]
	target.Arch = target.Vars["target.arch"]
	target.BuildProfile = target.Vars["target.build_profile"]

	// XXX: Verify required fields set?

	return nil
}

func (target *Target) Package() *pkg.LocalPackage {
	return target.basePkg
}

func (target *Target) Name() string {
	return target.basePkg.Name()
}

func (target *Target) ShortName() string {
	return filepath.Base(target.Name())
}

func (target *Target) App() *pkg.LocalPackage {
	dep, err := pkg.NewDependency(nil, target.AppName)
	if err != nil {
		return nil
	}

	appPkg := project.GetProject().ResolveDependency(dep)
	if appPkg == nil {
		return nil
	}

	return appPkg.(*pkg.LocalPackage)
}
{'fraction_non_alphanumeric': 0.11567398119122257, 'fraction_numerical': 0.0012539184952978057, 'mean_word_length': 4.0723370429252785, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2603348', 'n_tokens_mistral': 2119, 'n_tokens_neox': 2083, 'n_words': 630}