gml/internal/build/resources.go
2019-01-14 23:15:20 +01:00

102 lines
2.8 KiB
Go

/*
* GML - Go QML
*
* The MIT License (MIT)
*
* Copyright (c) 2019 Roland Singer <roland.singer[at]desertbit.com>
* Copyright (c) 2019 Sebastian Borchers <sebastian[at]desertbit.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package build
import (
"os"
"path/filepath"
"strings"
"text/template"
)
var qmlResTmpl = template.Must(template.New("t").Parse(qmlResData))
func prepareResources(ctx *Context) (err error) {
// Create the resources file.
f, err := os.Create(ctx.QMLResFile)
if err != nil {
return
}
defer func() {
derr := f.Close()
if derr != nil && err == nil {
err = derr
}
}()
files := make([]string, 0)
// Define a walk func used to walk over all files in our qml and resources dirs.
walkFunc := func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// Ignore any hidden files or directories.
if strings.HasPrefix(info.Name(), ".") {
if info.IsDir() {
return filepath.SkipDir
}
return nil
}
// Add any file that we encounter to the resources, but relative to our
// directory where the .qrc file lies within.
if !info.IsDir() {
files = append(files, strings.TrimPrefix(path, ctx.SourceDir+"/"))
}
return nil
}
// Scan first the qml directory and add all files found within
// to our resources file.
err = filepath.Walk(ctx.QMLDir, walkFunc)
if err != nil {
return
}
// Do the same for the resources directory.
err = filepath.Walk(ctx.QMLResDir, walkFunc)
if err != nil {
return
}
// Write the contents of our template with the paths to the file.
return qmlResTmpl.Execute(f, files)
}
const qmlResData = `<!-- This file is auto-generated by gml. -->
<!DOCTYPE RCC><RCC version="1.0">
<qresource prefix="/">
{{- range .}}
<file>{{.}}</file>
{{- end}}
</qresource>
</RCC>`