[ruby/yarp] Use templating to avoid duplicating the YARP version in many places

https://github.com/ruby/yarp/commit/9c359fd92e
This commit is contained in:
Benoit Daloze 2023-08-21 16:27:06 +02:00 коммит произвёл git
Родитель dd07b70253
Коммит f603497105
11 изменённых файлов: 38 добавлений и 23 удалений

Просмотреть файл

@ -523,6 +523,7 @@ require_relative "yarp/node"
require_relative "yarp/ripper_compat"
require_relative "yarp/serialize"
require_relative "yarp/pack"
require_relative "yarp/version"
if RUBY_ENGINE == "ruby" and !ENV["YARP_FFI_BACKEND"]
require "yarp/yarp"

Просмотреть файл

@ -171,8 +171,10 @@ module YARP
# the YARP module.
private_constant :LibRubyParser
# The version constant is set by reading the result of calling yp_version.
VERSION = LibRubyParser.yp_version.read_string
library_version = LibRubyParser.yp_version.read_string
if library_version != YARP::VERSION
raise "The YARP library version (#{library_version}) does not match the expected version (#{YARP::VERSION})"
end
def self.dump_internal(source, source_size, filepath)
LibRubyParser::YPBuffer.with do |buffer|

5
lib/yarp/version.rb Normal file
Просмотреть файл

@ -0,0 +1,5 @@
# frozen_string_literal: true
module YARP
VERSION = "0.8.0"
end

Просмотреть файл

@ -1,8 +1,10 @@
# frozen_string_literal: true
require_relative 'lib/yarp/version'
Gem::Specification.new do |spec|
spec.name = "yarp"
spec.version = "0.8.0"
spec.version = YARP::VERSION
spec.authors = ["Shopify"]
spec.email = ["ruby@shopify.com"]
@ -67,6 +69,7 @@ Gem::Specification.new do |spec|
"lib/yarp/pack.rb",
"lib/yarp/ripper_compat.rb",
"lib/yarp/serialize.rb",
"lib/yarp/version.rb",
"src/diagnostic.c",
"src/enc/yp_big5.c",
"src/enc/yp_euc_jp.c",

Просмотреть файл

@ -511,12 +511,12 @@ RUBY_FUNC_EXPORTED void
Init_yarp(void) {
// Make sure that the YARP library version matches the expected version.
// Otherwise something was compiled incorrectly.
if (strcmp(yp_version(), EXPECTED_YARP_VERSION) != 0) {
if (strcmp(yp_version(), YP_VERSION) != 0) {
rb_raise(
rb_eRuntimeError,
"The YARP library version (%s) does not match the expected version (%s)",
yp_version(),
EXPECTED_YARP_VERSION
YP_VERSION
);
}
@ -532,10 +532,6 @@ Init_yarp(void) {
rb_cYARPParseWarning = rb_define_class_under(rb_cYARP, "ParseWarning", rb_cObject);
rb_cYARPParseResult = rb_define_class_under(rb_cYARP, "ParseResult", rb_cObject);
// Define the version string here so that we can use the constants defined
// in yarp.h.
rb_define_const(rb_cYARP, "VERSION", rb_str_new2(EXPECTED_YARP_VERSION));
rb_define_const(rb_cYARP, "BACKEND", ID2SYM(rb_intern("CExtension")));
// First, the functions that have to do with lexing and parsing.

Просмотреть файл

@ -5,8 +5,6 @@
#include <ruby/encoding.h>
#include "yarp.h"
#define EXPECTED_YARP_VERSION "0.8.0"
VALUE yp_source_new(yp_parser_t *parser);
VALUE yp_token_new(yp_parser_t *parser, yp_token_t *token, rb_encoding *encoding, VALUE source);
VALUE yp_ast_new(yp_parser_t *parser, yp_node_t *node, rb_encoding *encoding);

Просмотреть файл

@ -0,0 +1,9 @@
#ifndef YARP_VERSION_H
#define YARP_VERSION_H
#define YP_VERSION_MAJOR <%= YARP_VERSION_MAJOR %>
#define YP_VERSION_MINOR <%= YARP_VERSION_MINOR %>
#define YP_VERSION_PATCH <%= YARP_VERSION_PATCH %>
#define YP_VERSION "<%= YARP_VERSION %>"
#endif // YARP_VERSION_H

Просмотреть файл

@ -48,10 +48,6 @@ public class Loader {
private ConstantPool constantPool;
private final Nodes.Source source;
private byte MAJOR_VERSION = (byte) 0;
private byte MINOR_VERSION = (byte) 8;
private byte PATCH_VERSION = (byte) 0;
private Loader(byte[] serialized, Nodes.Source source) {
this.buffer = ByteBuffer.wrap(serialized).order(ByteOrder.nativeOrder());
this.source = source;
@ -63,9 +59,9 @@ public class Loader {
expect((byte) 'R');
expect((byte) 'P');
expect(MAJOR_VERSION);
expect(MINOR_VERSION);
expect(PATCH_VERSION);
expect((byte) <%= YARP_VERSION_MAJOR %>);
expect((byte) <%= YARP_VERSION_MINOR %>);
expect((byte) <%= YARP_VERSION_PATCH %>);
// This loads the name of the encoding. We don't actually do anything
// with it just yet.

Просмотреть файл

@ -13,10 +13,6 @@ end
module YARP
module Serialize
MAJOR_VERSION = 0
MINOR_VERSION = 8
PATCH_VERSION = 0
def self.load(input, serialized)
Loader.new(Source.new(input), serialized).load
end
@ -64,7 +60,9 @@ module YARP
def load
raise "Invalid serialization" if io.read(4) != "YARP"
raise "Invalid serialization" if io.read(3).unpack("C3") != [MAJOR_VERSION, MINOR_VERSION, PATCH_VERSION]
if io.read(3).unpack("C3") != [<%= YARP_VERSION_MAJOR %>, <%= YARP_VERSION_MINOR %>, <%= YARP_VERSION_PATCH %>]
raise "Invalid serialization version"
end
@encoding = Encoding.find(io.read(load_varint))
@input = input.force_encoding(@encoding).freeze

Просмотреть файл

@ -4,6 +4,11 @@ require "erb"
require "fileutils"
require "yaml"
require_relative "../lib/yarp/version"
YARP_VERSION = YARP::VERSION
YARP_VERSION_MAJOR, YARP_VERSION_MINOR, YARP_VERSION_PATCH = YARP_VERSION.split(".")
COMMON_FLAGS = 1
class Param
@ -312,6 +317,7 @@ end
TEMPLATES = [
"ext/yarp/api_node.c",
"include/yarp/ast.h",
"include/yarp/version.h",
"java/org/yarp/Loader.java",
"java/org/yarp/Nodes.java",
"java/org/yarp/AbstractNodeVisitor.java",

Просмотреть файл

@ -13,6 +13,7 @@
#include "yarp/util/yp_char.h"
#include "yarp/util/yp_memchr.h"
#include "yarp/util/yp_strpbrk.h"
#include "yarp/version.h"
#include <assert.h>
#include <stdarg.h>