Commit 27390501 authored by Paul Asmuth's avatar Paul Asmuth
Browse files

bundle assets with xxd

parent 9ce8679f
Loading
Loading
Loading
Loading
+0 −0

Empty file added.

+1 −3
Original line number Diff line number Diff line
@@ -10,9 +10,7 @@ all: assets
	(cd target && cmake .. && make)

assets:
	mkdir -p stage/assets
	(cd ../.. && xxd -i assets/index.html) > stage/assets/asset_index.html.h
	(cd ../.. && xxd -i assets/fnordmetric-web.css) > stage/assets/fnordmetric_web.css.h
	./assets.sh

test: all
	@find target/tests -iname "test-*" | while read t; do (cd ../../ && build/cmake/$$t) || exit 1; done

build/cmake/assets.sh

0 → 100755
+34 −0
Original line number Diff line number Diff line
#/bin/bash

mkdir -p stage/assets

asset_file() {
  (cd ../.. && cat $@ | xxd -i)
  echo "};"
}

asset_uniq() {
  echo "static const unsigned char __$1_data[] = {"
}

asset_name() {
  echo "static fnordmetric::web::Assets::AssetFile __$1(\"$2\", __$1_data, sizeof(__$1_data));"
};

(
  asset_uniq "fnordmetric_index_html"
  asset_file "assets/index.html"
  asset_name "fnordmetric_index_html" "index.html"
) > stage/assets/asset_index.html.c

(
  asset_uniq "fnordmetric_web_css"
  asset_file "assets/codemirror.css" "assets/fnordmetric-web.css"
  asset_name "fnordmetric_web_css" "fnordmetric-web.css"
) > stage/assets/asset_fnordmetric_web.css.c

(
  asset_uniq "fnordmetric_web_js"
  asset_file "assets/codemirror.min.js" "assets/fnordmetric-js"
  asset_name "fnordmetric_web_js" "fnordmetric-web.js"
) > stage/assets/asset_fnordmetric_web.js.c
+24 −9
Original line number Diff line number Diff line
@@ -7,24 +7,39 @@
 * copy of the GNU General Public License along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 */
#include <unordered_map>
#include <string>
#include <fnordmetric/util/runtimeexception.h>
#include <fnordmetric/web/assets.h>
#include <asset_index.html.h>
#include <fnordmetric_web.css.h>

namespace fnordmetric {
namespace web {

#define TRY_FILE(F,T) \
  if (filename == F) { \
    return std::string((const char *) T, sizeof(T)); \
static std::unordered_map<std::string, std::pair<const unsigned char*, size_t>>
    asset_files;

Assets::AssetFile::AssetFile(
    const std::string& name,
    const unsigned char* data,
    size_t size) {
  printf("register: %s,", name.c_str());
  asset_files.emplace(name, std::make_pair(data, size));
}

std::string Assets::getAsset(const std::string& filename) {
  TRY_FILE("index.html", assets_index_html);
  TRY_FILE("fnordmetric-web.css", assets_fnordmetric_web_css);
  RAISE(util::RuntimeException, "asset not found: %s\n", filename.c_str());
  const auto asset = asset_files.find(filename);

  if (asset != asset_files.end()) {
    const auto& data = asset->second;
    return std::string((const char*) data.first, data.second);
  }

  RAISE(util::RuntimeException, "asset not found: %s", filename.c_str());
}

}
}

#include <asset_index.html.c>
#include <asset_fnordmetric_web.css.c>
#include <asset_fnordmetric_web.js.c>
+8 −0
Original line number Diff line number Diff line
@@ -10,12 +10,20 @@
#ifndef _FNORDMETRIC_WEB_ASSETS_H
#define _FNORDMETRIC_WEB_ASSETS_H
#include <string>
#include <vector>

namespace fnordmetric {
namespace web {

class Assets {
public:
  class AssetFile {
  public:
    AssetFile(
        const std::string& name,
        const unsigned char* data,
        size_t size);
  };

  static std::string getAsset(const std::string& filename);